Testing the Web App Locally
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
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.
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.
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.
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.
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.
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: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.
Become a Member to join the conversation.