How to Create and Host a Cloud Python 3 Web App FREE (Part 2)
Introduction
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.
- Show how we made Glitch run a Python 3 app, instead of a Node.js (JavaScript) app.
- Explain how an
aiohttp
web app is created (the code insideapp.py
)
An explaination
There currently three files in our application
app.py requirements.txt start.sh
requirements.txt
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.
aiohttp==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
start.sh
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 tellspip3
that we want to install something.--user
: This instructspip3
to only install the package for the currently logged in user (you), rather than globally-r requirements.txt
: the-r
instruction tellspip3
to use a text file with a list of the packages you want to install. We tell it to use therequirements.txt
file.
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.
python3 app.py
app.py
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
/beatswithbenji/
route. - 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 app.py
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 app.py
line in the start.sh
script).
if __name__ == '__main__':
Running 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 app.py
file.
Without the if __name__ == '__main__':
code in app.py
our imaginary second file would actually start running the app by only trying to import the handle_index
function.
Next in app.py
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.
web.run_app(run)
Next steps
This is a pretty useless app, so in part 3 I’ll show you how make it do something more useful.