PyJaipur / PyJudge

Simple Programming Contest hosting software
MIT License
17 stars 29 forks source link

Understanding working with bottle #12

Open kirtibajaj opened 5 years ago

kirtibajaj commented 5 years ago

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.

theSage21 commented 5 years ago

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

import bottle

app = bottle.Bottle()

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

app.run()

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 commented 5 years ago

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?

2) @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 commented 5 years ago
  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):