DEV Community

Cover image for python for the web
tcs224
tcs224

Posted on

python for the web

You can use the Python programming language to build web apps. These web apps then run server-side and the user is presented with the traditional web app (HTML, JavaScript, Angular, Vue).

Python doesn't have a built-in web framework, so you have to pick one. A web framework starts a server, does templates, deals with architecture (often model-view-controller) and so on. It saves you a lot of development time. Web frameworks can be installed with pip

pip install <framework>

Web Frameworks

A popular micro-framework is Flask (hello world example). This does the bare minimum, run the server, routing, parse templates, pass variables etc.

If you want to have many features available in the framework, like an ORM (Object Relational Manager) you could go for Django (django tutorial). Downside of Django is the learning curve and it has many features you may not need.

Tornado is another option you could pick. It is a Python web framework and asynchronous networking library. Tornado apps are scale-able, it can scale to tens of thousands of open connections.
You can install tornado with pip

pip install tornado

It's very straightforward to create a hello world app:

import tornado.ioloop
import tornado.web

class MainHandler(tornado.web.RequestHandler):
    def get(self):
        self.write("Hello, world")

def make_app():
    return tornado.web.Application([
        (r"/", MainHandler),
    ])

if __name__ == "__main__":
    app = make_app()
    app.listen(8888)
    tornado.ioloop.IOLoop.current().start()

It opens the server at port 8888, then waits for input. It creates one web route (/) which is linked to MainHandler. This returns the text "Hello, World".

Hosting

To get your app online, you have to host it on a server. You could open your home server to the web, but this is generally not a good idea.

An easy way to do hosting is to use PythonAnywhere. This lets you host, run and code in the cloud.

If you want to do everything yourself, you could use Vultr or Digital Ocean instead.

Top comments (0)