Git Product home page Git Product logo

python-webserver-quickstarts's Introduction

Python Web server Quickstarts

Run a "Hello World" webserver in a selection of Python frameworks

GitHub tag License

Made with Python

dependency - flask dependency - django dependency - cherrypy dependency - tornado

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.

Purpose

  • 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.

Approaches

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.

Frameworks

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.

Flask

  • 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:

Django

  • 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:

CherryPy

Not as famous as Flask, but it is similar and from some research it is supposed to be faster too.

Links:

Tornado

Links:

Requirements

  • Python >= 3.6

Installation

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.

Run

Start

$ 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

View the hello world response in the browser.

For Django, you also get an admin view.

Resources

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

License

Released under MIT by @MichaelCurrin.

python-webserver-quickstarts's People

Contributors

michaelcurrin avatar

Watchers

 avatar  avatar  avatar

Forkers

ritikparihar12

python-webserver-quickstarts's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.