How to Host Your Python Application

Python is an incredibly popular language for web development. If you’re interested in learning it or just want to experiment with incorporating Python into your projects, the quick-and-easy service PythonAnywhere will help get you started.

Host Python Web App

To use PythonAnywhere, all you need is a web browser and about 5 minutes of your time (and if you’re on a slow internet connection, maybe some patience as well). Just follow these simple steps:

Go to PythonAnywhere.

Sign up for a free account and fill out your details.

Then click on “open web apps”.

Click on open web apps

Then click on “add a new web app”.

New web app

You’ll see a page that lets you configure your app:

This page allows you to configure the Web framework you want to use. There are many different frameworks like Django, web2py, Flask, Bottle and others.

For simplicity, I’ll go with Flask.

Python Web framework

Then it lets you select your Python version. Go for the newest version.

Python version

Finally enter a Path for your Python file and click next. That sets up your Python web app and it is now hosted and online!

Flask web app

You can then open the source for of your web app. Either through the bash console or the web interface. They provide an online editor.

Python flask

Flask web app

By creating a web app, they automatically created a template for the Flask web app. The template looks like this:

# A very simple Flask Hello World app for you to get started with...

from flask import Flask

app = Flask(__name__)

def hello_world():
    return 'Hello from Flask!'

The first line of code simply loads the Flask module.

from flask import Flask

Then the app is created

app = Flask(__name__)

Then a route is created. A route is mapping of a link to a Python function. In the case the / link is called, Python will execute the function hello_world(). The return value is shown in the browser.

def hello_world():
    return 'Hello from Flask!'

You can change this value to anything you want. If you return html tags, the browser will simply render html.

You can add as many routes as you want. Every web url has the map to a unique Python function, for example try this:

def hello_world():
    return 'Hello from Flask!'

def html():
    return '<h1>html</h1>'

def image():
    return "<img src='">'

Of course there’s a lot more you can do with the Python Flask framework, these are just the absolute basics.

Flask routing

A url route is a mapping between an HTTP verb and an endpoint. The endpoint can be a resource, such as a file or script, or it can be a function that processes the request.

In Flask, routes are defined in the app object. Each route is associated with a handler, which is a Python function that takes an incoming request and produces an outgoing response.

def a_function():
    return 'Data shown in web browser!'

The first argument to the route decorator is the path of the URL. This can be a static string, or it can contain variable parts enclosed in angle brackets (<>). Variable parts are converted to positional arguments