TDM 10200: Project 13 — Spring 2024
Motivation: Flask is a web application framework. It provides tools, libraries, and technologies to build a web application.
Context: Create simple conceptual webpage using Flask
Scope: Python, Flask, Visual Studio Code
Readings and Resources
Questions
Question 1 (2 points)
-
Following the instructions on Visual Studio Code Starter Guide, launch VS Code on Anvil
-
Following the instructions on Visual Studio Code Starter Guide, including the Initial Configuration of VS Code, and install the Python extension for Visual Studio
-
Following the instructions on Visual Studio Code Starter Guide, select the Python interpreter path to
/anvil/projects/tdm/apps/lmodbin/python-seminar/python3
-
Open a new terminal in Visual Studio Code. Show the current path for the Python interpreter by typing
which python3
(it should show/anvil/projects/tdm/apps/lmodbin/python-seminar/python3
for the output).
(You can stop reading when you get to the section on "Debugging Python Code" in the starter guide.)
For Question 1a, follow the sections of "How can I launch VS Code on Anvil?" Choose the following options:
If you get "Do you trust the authors of the files in this folder" window pop up, click "Yes, I trust the authors" |
Refer to Code Editing in Visual Studio Code to see how to edit and run code |
Question 2 (2 points)
-
Create a python file in Visual Studio Code called
helloWorld.py
, simply to create and display a greeting sentence -
Run the file in the terminal using:
python3 helloWorld.py
Question 3 (2 points)
-
After you setup
Visual Studio Code
, create a simple flask application that only defines a Flask instance; call itapp
. It should look like this (make sure that you get the indenting correct:from flask import Flask app = Flask(__name__) if __name__ == '__main__': app.run(host='0.0.0.0', port=8050, debug=True)
-
Run the python file
|
Question 4 (2 points)
-
You should get a 404 page - "Not Found page" for your Question 3, since you have not (yet) defined a route for the home page. The browser likely will say "The requested URL was not found on the server. If you entered the URL manually please check your spelling and try again." Now update the code to add a route for the home page, as shown in the tip below.
-
Use a decorator for the home ("/") route, followed by a function you would like to run. It is ok if you just output a greeting message like "Hello World!" This should be added to your file, just before the if statement.
@app.route('/') def hello(): return "Hello World!"
-
-
Run the python file
If you already ran something in the Python terminal and you want it to stop running, you can click into the terminal and type Control-C. |
Question 5 (2 points)
-
You should get your message in a webpage in Question 4. Now let us (separately) add an html file, instead of putting the content for the message inside the python file. Create a folder named
templates
, in the same location where your python file is located, and then create a simple html file namedindex.html
in the folder, to hold the greeting message. (Or you may create a fancy html page, but you do not need to make it fancy!) -
Update your python file root decorator to render the information to the webpage from
index.html
-
Flask’s function called
render_template
is useful. You can import it by the following statement
from flask import Flask from flask import render_template
and you can change your definition of
hello
like this:def hello(name=None): return render_template('index.html', name=name)
-
-
Run the python file
Your For instance, Dr Ward’s html page looks like this:
|
Project 13 Assignment Checklist
-
Jupyter Lab notebook with your code, comments and output for the assignment
-
firstname-lastname-project13.ipynb
-
-
Python file with code and comments for the assignment
-
firstname-lastname-project13.py
-
-
Submit files through Gradescope
Please make sure to double check that your submission is complete, and contains all of your code and output before submitting. If you are on a spotty internet connection, it is recommended to download your submission after submitting it to make sure what you think you submitted, was what you actually submitted. In addition, please review our submission guidelines before submitting your project. |