How to Create and Host a Cloud Python 3 Web App FREE (Part 2)


In Part 1 I showed you how, using Glitch, you can create and host a simple Python 3 web app for free.

In this tutorial I’m going to run through two things.

  1. Show how we made Glitch run a Python 3 app, instead of a Node.js (JavaScript) app.
  2. Explain how an aiohttp web app is created (the code inside

An explaination

There currently three files in our application


requirements.txt screenshot

By convention most Python 3 applications contain a requirements.txt file. This file lists the external dependencies, also known as packages, that the application needs to be able to run.

For now all we require is the aiohttp library, and we’re using version 2.3.10.


Incidently, because Glitch was designed to run Node.js (JavaScript) apps, it’s best to start any Glitch Python 3 application by creating this file, as it forces Glitch to know that you’re wanting to run a Python app, not a Node.js application.

Once Glitch sees this file it will automatically attempt to install the modules that you have listed in the file. By default it uses Legacy Python (Python 2.7) to do this, so in our example it will fail because aiohttp doesn’t support Legacy Python. This is ok, because with the next file we’ll tell the app to use Python 3 instead.aio

After Glitch finds the requirements.txt file to turn it into a Python app it then looks for a file named This is known as a shell script, and is what Glitch uses to know how to start running the app.

The first line in our file runs the command pip3 which is the tool commonly used to install external Python 3 packages.

pip3 install --user -r requirements.txt

Once the above pip3 command is finished, we’ll have aiottp version 2.3.10 installed, ready for our Python 3 app to use.

The second and final line of our shell script tells the server to use Python 3 to run the Python file, which is where our main code is. NOTE: Because we want to use Python 3, and not Legacy Python it’s important to use python3, and not just python on this line.

python3 screenshot

This is where the guts of our application lives. First I’ll need to define a few terms I’ll use throughout the rest of these tutorials.

  1. Routes: By convention you’ll often refer to the main route as the index. For example is the index as opposed to, which would be the /beatswithbenji/ route.
  2. Handlers: In the aiohttp world the functions that handle what happens when a user makes a request to a particular route is called… a handler.

The very first line of code in tells Python we want to use the web component of the aiohttp package.

from aiohttp import web

The next two lines define a simple Python 3 function, or handler, named handle_index. The use of async def instead of just def means it’s an asynchronous function meaning you can run more than one of these at once. We’ll get more in depth into that in further tutorials. This function takes in one argument, request, and returns a web.Response object with the text “My First Async Python 3 Web App”.

async def handle_index(request):
    return web.Response(text='My First Async Python 3 Web App')

The final lines in our script do a few things. First we make sure we only actually run the app if we call the Python file directly (i.e. the python3 line in the script).

if __name__ == '__main__':

Running python3 is different from having a second Python file, let’s call it, where we might want to import something from the file. For example

from app import handle_index

The above pretend file imports the handle_index function from the file.

Without the if __name__ == '__main__': code in our imaginary second file would actually start running the app by only trying to import the handle_index function.

Next in we create an aiohttp web application object.

    app = web.Application()

Then we add add a route, and assign a handler to it.

    app.router.add_get('/', handle_index)

Note: because we’re using an older version of Python 3 (3.5) we have to use an old version of aiohttp (2.3.10) which uses a different way of assigning routes than the latest aiohttp.

Finally we actually run the application by calling the run_app function using the web component we imported aiohttp in the first line of the file.


Next steps

This is a pretty useless app, so in part 3 I’ll show you how make it do something more useful.

Follow me on Twitter

My homepage

My podcast