| [ Web Proxy ] |
| Viewing: https://code.visualstudio.com/docs/python/tutorial-flask.md | [Back] [Original] |
© 2018
Home page for the Visual Studio Code Flask tutorial.
`, then save the file. These lines are the only unique parts of the extended page template: 1. In the `templates` folder, create `about.html`, use the snippet to insert the boilerplate markup, insert `About us` and `About page for the Visual Studio Code Flask tutorial.
` in the "title" and "content" blocks, respectively, then save the file. 1. Repeat the previous step to create `templates/contact.html` using `Contact us` and `Contact page for the Visual Studio Code Flask tutorial.
` in the two content blocks. 1. In `app.py`, add functions for the /about/ and /contact/ routes that refer to their respective page templates. Also modify the `home` function to use the `home.html` template. ```python # Replace the existing home function with the one below @app.route("/") def home(): return render_template("home.html") # New functions @app.route("/about/") def about(): return render_template("about.html") @app.route("/contact/") def contact(): return render_template("contact.html") ``` ### Run the app With all the page templates in place, save `app.py`, run the app, and open a browser to see the results. Navigate between the pages to verify that the page templates are properly extending the base template.  >**Note**: If you're not seeing the latest changes, you might need to do a hard refresh on the page to avoid seeing a cached file. ## Optional activities The following sections describe additional steps that you might find helpful in your work with Python and Visual Studio Code. ### Create a requirements.txt file for the environment When you share your app code through source control or some other means, it doesn't make sense to copy all the files in a virtual environment because recipients can always recreate the environment themselves. Accordingly, developers typically omit the virtual environment folder from source control and instead describe the app's dependencies using a `requirements.txt` file. Although you can create the file by hand, you can also use the `pip freeze` command to generate the file based on the exact libraries installed in the activated environment: 1. With your chosen environment selected using the **Python: Select Interpreter** command, run the **Terminal: Create New Terminal** command (`kb(workbench.action.terminal.new)`)) to open a terminal with that environment activated. 1. In the terminal, run `pip freeze > requirements.txt` to create the `requirements.txt` file in your project folder. Anyone (or any build server) that receives a copy of the project needs only to run the `pip install -r requirements.txt` command to reinstall the packages in the original environment. (The recipient still needs to create their own virtual environment, however.) > **Note**: `pip freeze` lists all the Python packages you have installed in the current environment, including packages you aren't currently using. The command also lists packages with exact version numbers, which you might want to convert to ranges for more flexibility in the future. For more information, see [Requirements Files](https://pip.pypa.io/en/stable/user_guide/#requirements-files) in the pip command documentation. ### Refactor the project to support further development Throughout this Flask tutorial, all the app code is contained in a single `app.py` file. To allow for further development and to separate concerns, it's helpful to refactor the pieces of `app.py` into separate files. 1. In your project folder, create a folder for the app, such as `hello_app`, to separate its files from other project-level files like `requirements.txt` and the `.vscode` folder where VS Code stores settings and debug configuration files. 1. Move the `static` and `templates` folders into `hello_app`, because these folders certainly contain app code. 1. In the `hello_app` folder, create a file named `views.py` that contains the routings and the view functions: ```python from flask import Flask from flask import render_template from datetime import datetime from . import app @app.route("/") def home(): return render_template("home.html") @app.route("/about/") def about(): return render_template("about.html") @app.route("/contact/") def contact(): return render_template("contact.html") @app.route("/hello/") @app.route("/hello/") def hello_there(name = None): return render_template( "hello_there.html", name=name, date=datetime.now() ) @app.route("/api/data") def get_data(): return app.send_static_file("data.json") ``` 1. In the `hello_app` folder, create a file `__init__.py` with the following contents: ```python import flask app = flask.Flask(__name__) ``` 1. In the `hello_app` folder, create a file `webapp.py` with the following contents: ```python # Entry point for the application. from . import app # For application discovery by the 'flask' command. from . import views # For import side-effects of setting up routes. ``` 1. Open the debug configuration file `launch.json` and update the `env` property as follows to point to the startup object: ```json "env": { "FLASK_APP": "hello_app.webapp" }, ``` 1. Delete the original `app.py` file in the project root, as its contents have been moved into other app files. 1. Your project's structure should now be similar to the following:  1. Run the app in the debugger again to make sure everything works. To run the app outside of the VS Code debugger, use the following steps from a terminal: 1. Set an environment variable for `FLASK_APP`. On Linux and macOS, use `export set FLASK_APP=webapp`; on Windows use `$env:FLASK_APP=webapp` if you're using PowerShell, or `set FLASK_APP=webapp` if you're using Command Prompt. 1. Navigate into the `hello_app` folder, then launch the program using `python -m flask run`. ### Create a container for a Flask app using the Container Tools extension The [Container Tools extension](https://marketplace.visualstudio.com/items?itemName=ms-azuretools.vscode-containers) makes it easy to build, manage, and deploy containerized applications from Visual Studio Code. If you're interested in learning how to create a Python container for the Flask app developed in this tutorial, check out the [Python in a container](/docs/containers/quickstart-python.md) tutorial, which will walk you through how to: - Create a `Dockerfile` file describing a simple Python container. - Build, run, and verify the functionality of a [Flask](https://flask.palletsprojects.com) app. - Debug the app running in a container. If you have any problems, you can search for answers or ask a question on the [Python extension Discussions Q&A](https://github.com/microsoft/vscode-python/discussions/categories/q-a). ## Next steps Congratulations on completing this walkthrough of working with Flask in Visual Studio Code! The completed code project from this tutorial can be found on GitHub: [python-sample-vscode-flask-tutorial](https://github.com/microsoft/python-sample-vscode-flask-tutorial). Because this tutorial has only scratched the surface of page templates, refer to the [Jinja2 documentation](https://jinja.palletsprojects.com) for more information about templates. The [Template Designer Documentation](https://jinja.palletsprojects.com/templates/#synopsis) contains all the details on the template language. You might also want to review the [official Flask tutorial](https://flask.palletsprojects.com/tutorial) as well as the documentation for Flask [extensions](https://flask.palletsprojects.com/extensions/). To try your app on a production website, check out the tutorial [Deploy Python apps to Azure App Service using Docker Containers](https://learn.microsoft.com/azure/developer/python/tutorial-deploy-containers-01). Azure also offers a standard container, [App Service on Linux](https://learn.microsoft.com/azure/developer/python/configure-python-web-app-local-environment), to which you deploy web apps from within VS Code. You may also want to review the following articles in the VS Code docs that are relevant to Python: - [Editing Python code](/docs/python/editing.md) - [Linting](/docs/python/linting.md) - [Managing Python environments](/docs/python/environments.md) - [Debugging Python](/docs/python/debugging.md) - [Testing](/docs/python/testing.md)| Web Proxy Viewer | New URL | Original Page |