Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Understanding working with bottle #12

Open
kirtibajaj opened this issue Jan 13, 2019 · 3 comments
Open

Understanding working with bottle #12

kirtibajaj opened this issue Jan 13, 2019 · 3 comments
Labels
Discussion/Doubts/Tutorial A discussion to understand some topic

Comments

@kirtibajaj
Copy link
Contributor

kirtibajaj commented Jan 13, 2019

Bottle is a fast, simple and lightweight WSGI micro web-framework for Python. It is distributed as a single file module and has no dependencies other than the Python Standard Library.

  • Routing: Requests to function-call mapping with support for clean and dynamic URLs.
  • Templates: Fast and pythonic built-in template engine and support for mako, jinja2 and cheetah templates.
  • Utilities: Convenient access to form data, file uploads, cookies, headers and other HTTP-related metadata.
  • Server: Built-in HTTP development server and support for paste, fapws3, bjoern, gae, cherrypy or any other WSGI capable HTTP server.
@theSage21 theSage21 added the Discussion/Doubts/Tutorial A discussion to understand some topic label Jan 14, 2019
@theSage21
Copy link
Member

Bottle is what is called a Web Server Gateway Interface (WSGI) framework. Let's take a tour through the Internet.

  • when people first discovered that you could transfer files between computers they did it like they did any other thing. Via foot. Someone wrote a file to a floppy disk and then walked/drove to a friend's house and delivered the floppy. This was called sneaker-net (if it was first done in India it might have been called chappal-net).
  • When computer networks were invented, people did not have to walk over. This gave rise to a new problem. The people you were sharing your files with were no longer just your friends. It could be anyone in the world. They had no idea what was on your computer. Some way to list files was needed in addition to sending the file to the person on their request.
    • A natural way was for people to be able to see inside someone's computer. Sort of a remote file explorer. Since during those times the command line was all that was popular, the obvious solution was a remote, text based file explorer.
    • you can try this out yourself. If you have python 3 open a command line and go to some folder. When you do python -m http.server it will give you a link like localhost:8000. If you open that in your browser it shows you a file and folder listing
  • This was all very good if you only ever wanted to show the same things again and again (for example a HTTP based book).
  • Things changed when things like facebook were designed (facebook was not the first).
  • facebook.com is either your home page or a login page depending on whether you are logged in or not. The ability to show different HTML for the same URL could not be handled by a file system like web server. A dynamic server was needed.
  • After a long evolution involving things like CGI/php etc, a neat solution called WSGI was created.
  • What wsgi does is define a standard way to introduce this dynamic ability.
    • when a user goes to a url, the full request that the user made is given to a function.
    • That function must then return a valid HTTP response which is then returned to the user.
  • Bottle makes this last part easier. For example, if you want to show 'hello' at the url /say/hello in bottle, what you would do is:
import bottle

app = bottle.Bottle()

@app.get('/say/hello')
def function_which_says_hello():
    return 'hello'

app.run()
  • First you create an app(application. This is needed by the WSGI system)
  • You tell the app that whenever someone wants to GET the url /say/hello, run the function function_which_returns_hello
  • The function returns a simple python string. Bottle realizes that this is not a proper HTTP response and builds a proper response from that string before returning it to the person who made the request
  • finally you run the web server. Bottle has it's own web server for development purposes. You might have heard of others

What happens when you ask for a location(URL) like github.com/PyJaipur, a program running on some computer controlled by the company github decides what HTML to send to you.

@arnabsinha99
Copy link
Contributor

  1. path = os.path.abspath(__file__)

What does __file__ in the above line do? Also, I read that abspth() returns an absolutized version of pathname "path". What does absolutized mean?

  1. @app.route('/hello/<something>')
    def index(something):
    return template('<b>Hello My Name is {{something}}</b>', something = 'Shivank')

This is a snippet of @shivank98 's code of server.py . There is no such page as hello/<something> . Then where is this piece of code being implemented?

@theSage21
Copy link
Member

theSage21 commented Jan 16, 2019

  1. I think you are talking about server.py#L4.
    • Python has a few inbuilt variables. When you run a file with python server.py, a variable called __file__ is created which holds the location of the file that you are running.
    • Paths in most operating systems start at some top level. C:\System\something\something\Desktop in windows or /home/user.
    • Paths can also be relative like ../abc or ./some/folder where .. means the folder before and . means current.
    • these paths can be made absolute. For example ./PyJaipur can be made /home/user/PyJaipur
  2. This refers to these 3 lines
    • That code can be safely removed as it's not relevant to the project.
    • There, the function template is being used and the HTML template is provided as the first argument. That's why there is no separate HTML file. If you run the server and go to the url /hello/arjoonn that function will run.

Does that answer your question?

Further reading( In the future when you have time, I fully recommend trying to read both documents fully. Lots of interesting stuff):

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Discussion/Doubts/Tutorial A discussion to understand some topic
Projects
None yet
Development

No branches or pull requests

3 participants