In this project we will create a simple web server entirely in Python. We will use Flask, a simple and lightweight framework.
This project is aimed intended for GCSE Computing students (although I hope it will be helpful to a general audience). There are student workbooks available as part of the schoolcoders subscription.
Starting from a simple static page, we will:
- introduce HTML templates, and add CSS styling
- create several pages with dynamic content
- access the site from another computer
- use an SQLite database to create list and detail pages
- create and online poll
These instructions are for a Windows PC running Python 3.x. The instructions should apply to Linux, including Raspberry Pi, except that the method for installing the Python Flask module will be different. Check how to install modules for your flavour of Linux (eg using apt-get for Debian).
Open a command (cmd) window and use pip to install Flask:
pip install flask
You should first create a _working folder_ for the project, somewhere suitable. For example c:\web-server, although you will probably want to create it where ever you keep the rest of your work.
Create a new file in your Python editor (IDLE, or whatever editor you prefer). Type in the following code:
from flask import Flask app = Flask(__name__) @app.route('/') def index(): return 'My first Flask site' if __name__ == '__main__': app.run(debug=True, port=80, host='0.0.0.0')
Save the code as _server.py_ in your working folder. Then run the code. You should see something like this in the IDLE console:
RESTART: C:\web-server\server.py * Running on http://0.0.0.0:80/ (Press CTRL+C to quit) * Restarting with stat
Your web server is running!
View the page in a browser
Open a browser and type the following into the address field:
This is a special URL. Instead of going out on to the web to find a page, it looks for a web server on your own PC. With most browsers you can shorten this to 127.0.0.1, and it will still work. Or, instead of the numbers, you can just type in _localhost_ if you find it easier to remember. They all do the same thing.
If all is well, you should see your message (‘My first Flask site’) in your browser.
What the code does
Here is the code, line by line. First we import the Flask module:
from flask import Flask
Next we create a Flask object, called app. This object runs the web server. We pass in name, the name of the module. This tells Flask where to look for the extra files we will add later - don’t worry about this too much, you would only need to play with this parameter to create complex web sites.
app = Flask(__name__)
Now we get to the function index() which actually creates the web page content. In this case it is very simple, just the text My first Flask site.
The line @app.route(‘/’), just before the index function, is called a decorator. It tells Flask that any path which matches ‘/’ should use the index() function to create its content. The path ‘/ ’ is the home page of the website.
@app.route('/') def index(): return 'My first Flask site'
The final part should be familiar, it just causes app.run if this file is executed as a python program. The parameters are set to default values which allow a browser to view our page.
if __name__ == '__main__': app.run(debug=True, port=80, host='0.0.0.0')
The full code for this section can be found on github in the folder web-server/basic-server.