Loading video player…

Building a Basic Python Web App

00:00 Build a Basic Python Web Application.

00:04 Google App Engine requires you to use a web framework for creating your web application in a Python 3 environment. Since you’re trying to use a minimal setup to get your local Python code up on the Internet, a microframework such as Flask is a good choice.

00:19 A minimal implementation of Flask is so small that you might not even notice that you’re using a web framework. The application you’re going to create will rely on several different files, so the first thing to do is to create a project folder to make sure everything is in the same place.

00:36 Create a project folder and give it a name that’s descriptive of your project. For this practice project, call the folder hello-app. You’ll need three files inside this folder, as seen on-screen.

00:50 main.py contains your Python code wrapped in a minimal implementation of the Flask web framework. requirements.txt lists all of the dependencies your code needs to work properly.

01:03 app.yaml helps Google App Engine decide which settings to use on its server. While three files might sound a lot, you’ll see that this project uses fewer than ten lines of code across all of them.

01:16 This represents the minimal setup you need to provide to Google App Engine for any Python project you may launch. The rest will be your own Python code.

01:26 Next, you’ll take a look at the content of each of the files, starting with the most complex one, main.py. main.py is the file that Flask uses to deliver the content.

01:38 First, you import the Flask class. Next, you create an instance of a Flask app.

01:47 After creating the Flask app, you write a Python decorator called @app.route that Flask uses to connect URL endpoints with code contained in functions.

01:58 The argument to @app.route defines the URL path component, which is the root path in this case. The next code makes up index(), which is wrapped by that decorator.

02:09 This function defines what should be executed if the defined URL endpoint is requested by a user. Its return value determines what a user will see when they load the page. Note that the naming of index() is only a convention.

02:24 It relates to how the main page of a website is often called index.html. You can choose a different function name if you want.

02:33 If a user types the base URL of your web app into their browser, then Flask will run index() and the user will see the returned text. In this case, the text is just one sentence: Congratulations, it's a web app! You can render more complex content, and you can also create more than one function so that users can visit different URL endpoints in your app to receive different responses. However, for this initial implementation, it’s fine to stick with a short and encouraging success message.

03:05 The next file to look at is requirements.txt. Since Flask is the only dependency of your project, that’s all you’ll need to specify.

03:17 If your app has other dependencies, then you’ll need to add them to your requirements.txt file as well. Google App Engine will use requirements.txt to install the necessary Python dependencies for your project when setting it up on the server.

03:30 This is similar to what you would do after creating and activating a new virtual environment locally.

03:38 The third file, app.yaml, helps Google App Engine set up the right server environment for your code. This file requires only one line, which defines the Python runtime.

03:52 The line shown on-screen clarifies that the right runtime for your Python code is Python 3.9. This is enough for Google App Engine to do the necessary setup on its servers.

04:04 It’s worth checking that the Python 3 Runtime Environment you want to use is available on Google App Engine. At the time of creating this course, Python 3.10 was the latest release version of Python, but Python 3.9 was the latest version available on Google App Engine.

04:21 You can use Google App Engine’s app.yaml file for additional setup, such as adding environment variables to your application. You can also use it to define the path to static content for your app, such as images, CSS, or JavaScript files.

04:35 This course won’t go into these additional settings, but you can consult Google App Engine’s documentation on the app.yaml Configuration File if you want to add such functionality.

04:47 These nine lines of code complete the necessary setup for this app. Your project is now ready for deployment. However, it’s good practice to test your code before putting it into production so you can catch potential errors.

05:02 Next, you’ll check whether everything works as expected locally before deploying your code to the Internet.

Become a Member to join the conversation.