Skip to main content

How to Create a Web Server in Python (Flask)

In this article, I will show you how to create a Web server using Python.

There are a number of ways to create a Web server using Python.  If you want to create something simple, like a microservice, all you need is Flask.

info

These steps were tested on a Mac.

Step 1. Setup requirements

For this article, I am using:

  • Visual Studio Code (VS Code)
  • Docker
  • Dev Container Extension in VS Code
  • Python Version 3.10 or higher

If you are unfamiliar with dev containers, see my article: How to use Microsoft Dev Containers (Visual Studio Code, Python)

Step 2. Create a project folder

  • Create a project folder:
mkdir -p ~/projects/python/flask-demo
cd ~/projects/python/flask-demo

Step 3. Start Docker

  • Start Docker (open -a Docker)

  • Open VS Code (note dot)

code .

Step 4. Add a .gitignore file

For this step, you will need a .gitignore extension, like the one found here:

Once that extension is installed, do the following:

  • Add a Python-specific .gitignore file
  • In VS Code, call up the palette with this key combo: Cmd-Shift-P
  • Type in Add gitignore and select it
  • Type in Python and select it

This will generate a Python-specific .gitignore file.

Step 5. Add Dev Container

  • If you don't have the dev container extension installed - see my article referenced previously
  • Open the command palette (Cmd-Shift-P)
  • Search for: Dev Containers: Add Dev Container Configuration Files
  • Select Python 3 as the container configuration template
  • If not listed, select Show All
  • Select the default version (3.11 or higher)
  • Click OK for any other default options
  • When it appears, click Reopen in Container
  • Open up the terminal window in VS Code (Ctrl-` or View / Terminal)
tip

To run inside the dev container, execute all commands inside the VS Code terminal window. But if you run into issues, like accessing Github, etc. – use an external terminal window for those commands.

Step 6. Install Flask

From within the VS Code terminal window, run this command to install Flask:

pip install flask

Step 7. Create an app file

Create a new file in the root of your project called app.py.

On a Mac, you can create the new file with this command:

touch app.py

Open app.py in the editor, paste in this code, and save the file:

from flask import Flask,jsonify

# flask run

app = Flask(__name__)

@app.route('/', methods = ['GET'])
def health():
with open('/proc/uptime', 'r') as f:
uptime_seconds = float(f.readline().split()[0])

data = {
'app': __name__,
'uptime': uptime_seconds
}
return jsonify(data)

@app.route('/hello')
def hello_world():
return "<p>Hello, World!</p>"

The code does the following:

  • Imports Flask from the flask package
  • Also imports jsonify from the flask package
  • Sets the variable app to a new Flask instance

Root response

  • The combination of the decorator and the function (health()) defines an HTTP GET response for the root route (/)
  • The function gets the uptime of the app
  • forms a data structure for a response
  • uses jsonify to convert the structure to a JSON response

Second route

  • The second route defined simply returns a string of HTML to the browser.

Step 8. Run the app

In the VS Code terminal window, run the app with this command:

flask run
  • If the default port (5000) is not available you can override it with the --port flag, like this:
flask run --port 3003
  • If things are working correctly, click the Open in Brower button when it appears
  • You should see a JSON response, similar to this (your uptime should vary):
{"app":"app","uptime":199.19}
  • Browse to the other endpoint by adding it's path to the address in the browser:
http://127.0.0.1:5000/hello

You should see the simple Hello, World! message in the browser

Example Project

You can find the example project here:

I've added notes to the project's README about how to freeze the installation settings and restore them via the Dev container.

Conclusion

In this article, you learned how to:

  • Setup a Python dev container
  • Install Flask, a simple Web server that is useful for creating microservices, etc
  • Return JSON from a Flask server
  • Return HTML from a Flask server
  • Run the server with VS Code
  • Test the server in the browser

References