Run a "Hello World" webserver in a selection of Python frameworks
The library badges above will take you to PyPi if you click them.
This project includes short scripts for basic web servers in various frameworks. These can be used as references for comparison or interest. They can also be run, after installing server-specific dependencies.
- Reference of how to setup a basic server to help with other projects.
- Run each locally as a demo.
- Optionally update it locally to see what changes, like adding an endpoint.
The idea is to cover a mixture of approaches.
- HTML templating
- Use Jinja or similar templating engine to put data in HTML page and returned the finished result to the client.
- Model View Controller for more complex project with a database.
- Backend and frontend.
- Have an API endpoint of JSON data.
- Setup a page with outline and no data.
- Pull in the data from the API using JavaScript.
The scripts in this repo mostly come from quickstart guides on the official docs or blog tutorials.
Whatever framework you choose, you can use it for your hobby or production app but you'll probably want to setup a load balanced like Nginx in front of it. The frameworks tend to recommend this in your docs.
You might also want to reduce resources by implementing caching one of these:
- Database queries (reduce load on database).
- Endpoint or page responses (reduce load on Python app).
- Nginx cache settings.
- Add AWs CloudFront with caching.
- A micro-framework with this light and easy to extend with your own code or Flask extensions (separate libraries).
- Great for beginners.
- Build a backend API - handle and return requests typically using RESTful GET and POST endpoints and JSON data.
- Build a templating app.
- Use a database on the backend and HTML on the frontend.
- Use the Liquid / Jinja templating engine to read HTML templates and render them using loops and reusable "macros" (functions defined as Liquid).
- Handles connecting to a database like SQLite, MySQL or Postgres. For larger projects, you might want to install a package that you choose as your database ORM - like SQLAlchemy or SQLObject.
Links:
- Flask docs
- Homepage
- Tutorals in the Flask docs
- Define and Access the Database tutorial on Flask docs.
The application will use a SQLite database to store users and posts. Python comes with built-in support for SQLite in the sqlite3 module.
- Tutorials
- Retrieving HTML From data using Flask tutorial - return data as HTML with
render_template
. - Flask - SQLite tutorial on Tutorials Point.
- Tutorials point - including guides for routing, templates, sessions, static files, redirects and more.
- Retrieving HTML From data using Flask tutorial - return data as HTML with
- flask-json library's docs.
- Flask RESTful library.
- An opinionated framework that is built to handle a database, templating, admin view and auth without installing further libraries yourself.
- Django is not powerful but complex so not beginner-friendly.
- Additional Django libraries are available as replacements to handle things like GraphQL or ecommerce.
Links:
- Home - djangoproject.com
- Getting started official guide
- Django Hello World guide. This was used to create the app in this project.
- Starting a Django project
- Django REST tutorial
- Hello World App on Django For Beginners site
- Run Django without a database
Not as famous as Flask, but it is similar and from some research it is supposed to be faster too.
Links:
Links:
- Home - tornadoweb.org including quickstart.
- Python >= 3.6
Note: This instructions to install and run for a Linux and macOS environments.
Follow this guide to install/update Python and install project dependencies in a new virtual environment using requirements.txt file.
$ cd webservers
Run a server using one of the commands below. Note that most of the files are executable so do not need as python
command preceding them.
- No framework
$ ./no_framework_hello_world.py
- Flask
$ # Recommended way. $ FLASK_APP=flask_hello_world.py flask run $ # This also works: $ python flask_hello_world.py
- Django
$ cd django_hello_world $ python manage.py runserver 5000 $ # For help on available commands: $ python manage.py help
- CherryPy
$ cd cherrypy_hello_world $ hello_world/main.py $ multiple_endpoints/main.py
- Tornado
$ ./tornado_hello_world.py
Press CTRL+C when you want to stop the server.
View the hello world response in the browser.
For Django, you also get an admin view.
See also this site which includes tutorials for Python webserver frameworks - fullstackpython.com/web-frameworks.html.
There instructions were followed to create the base Django project initially, before customizing it. Run these to get a fresh app setup.
$ pip install django
$ django-admin startproject hello_app
$ cd hello_app
$ python manage.py startapp hello
Released under MIT by @MichaelCurrin.