Testing the Web App Locally
00:00
Testing Locally. Flask comes packaged with a development web server. You can use this development server to double-check that your code works as expected. To be able to run the Flask development server locally, you’ll need to complete two steps. Google App Engine will do the same steps on its servers once you deploy your code. Firstly, set up a virtual environment, and secondly, install the flask
package.
00:28 To set up a Python 3 virtual environment, navigate to your project folder on your terminal and type the following command.
00:41
This will create a new virtual environment named venv
using the version of Python 3 that you have installed on your system.
00:49 Next, you’ll need to activate the virtual environment. Here’s how you do it on macOS and Linux.
01:04 And here’s how it’s done in Windows, which is subtly different.
01:16 After executing this command, your prompt will change to indicate that you’re now operating from within the virtual environment. After you successfully set up and activate your virtual environment, you’re ready to install Flask.
01:28
This command fetches all packages listed in requirements.txt
from PyPI and installs them in your virtual environment. In this case, Flask will be installed alongside any packages that Flask depends on.
01:42
Wait for the installation to complete, then open up main.py
and add the following two lines of code at the bottom of the file.
02:01 These two lines tell Python to start Flask’s development server when the script is executed from the command line. It’ll be used only when you run the script locally.
02:11 When you deploy the code to Google App Engine, a professional web server process, such as Green Unicorn, will serve the app instead. You won’t need to change anything to make this happen.
02:23 You can now start Flask’s development server and interact with your Python app in your browser. To do so, you need to run the Python script that starts the Flask app by typing the following command.
02:39 Flask starts up the development server, and your terminal will display output similar to the text shown on-screen. This output tells you three important pieces of information. Firstly, a warning that this is Flask’s development server, which means you don’t want to use it to serve your code in production.
02:56
Fortunately, Google App Engine will handle that for you instead. Secondly, the URL where you can find your app. It’s the URL for localhost
, which means the app is running on your own computer.
03:08
Navigate to that URL in your browser to see the code live. Thirdly, press CTRL+C to quit
. This line tells you that you can exit the development server by pressing Control + C on your keyboard.
03:23
Follow the instructions and open a browser tab at 127.0.0.1:8080
, as seen on-screen. You should see a page displaying the text that your function returns: Congratulations, it’s a web app! Note the URL 127.0.0.1
is also called the localhost, which means that it points to your own computer.
03:48
The number 8080
that follows after the colon is called the port number. The port can be thought of as a particular channel, similar to broadcasting a television or radio channel.
04:00
You’ve defined these values in app.run
in your main.py
file. Running the application on port 8080
means that you can tune in to this port number and receive communications from the development server.
04:11
Port 8080
is commonly used for local testing, but you could also use a different number. You can use Flask’s development server to inspect any changes that you make to the code of your Python app.
04:24 The server listens to changes that you make in the code and will automatically reload to display them. If your app doesn’t render as you expect it to on the development server, then it won’t work in production either.
04:36 So make sure that it looks good before you deploy it. Also keep in mind that even if it does work well locally, it might not work quite the same once deployed.
04:46 This is because there are other factors involved when you deploy your code to Google App Engine. However, for a basic app, such as the one you’re building in this course, you can be confident that it’ll work in production if it works well locally.
04:59 Once you’ve checked your setup and the code’s functionality on your local development server, you’re ready to deploy it to Google App Engine, and that’s what’s covered next.
Bartosz Zaczyński RP Team on July 28, 2022
@ianmcd34 That error means Python can’t find the flask
module imported from your main.py
script. Did you install the Flask framework into the right virtual environment?
Become a Member to join the conversation.
ianmcd34 on July 28, 2022
When I run
main.py
, I getModuleNotFoundError no mudule named 'flask'