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.
- Explain how an
aiohttpweb app is created (the code inside
There currently three files in our application
app.py requirements.txt start.sh
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.
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
start.sh. 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
start.sh file runs the command
pip3 which is the tool commonly used to install external Python 3 packages.
pip3 install --user -r requirements.txt
install: This tells
pip3that we want to install something.
--user: This instructs
pip3to only install the package for the currently logged in user (you), rather than globally
-r requirements.txt: the
pip3to use a text file with a list of the packages you want to install. We tell it to use the
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 app.py 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.
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.
- Routes: By convention you’ll often refer to the main route as the index. For example https://irumble.com/ is the index as opposed to https://irumble.com/beatswithbenji/, which would be the
- Handlers: In the
aiohttpworld 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
app.py tells Python we want to use the
web component of the
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 app.py line in the
if __name__ == '__main__':
python3 app.py is different from having a second Python file, let’s call it
second_file.py, where we might want to import something from the
app.py file. For example
# second_file.py from app import handle_index
The above pretend file imports the handle_index function from the
if __name__ == '__main__': code in
app.py our imaginary second file would actually start running the app by only trying to import the
app.py we create an
aiohttp web application object.
app = web.Application()
Then we add add a route, and assign a handler to it.
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.
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