COMP0034 2023-24 Week 1 coding activities
You are strongly advised to start these before the tutorial so that you have time to learn the basics and consider any questions you may have.
-
Fork the repository. This creates a copy in your GitHub account.
-
Clone the repository from your GitHub account to your IDE.
-
Create and activate a virtual environment in the project's folder in your IDE.
-
Install Flask in the virtual environment e.g.
pip install Flask
-
Enter the command
pip list
in the terminal. You should see that Flask and several dependencies have been installed e.g. Jinja2, MarkupSafe, Werkzeug, blinker, click, itsdangerous
-
Open flask_app1/app1.py and read the comments that explain what each line does.
-
Run the Flask app using the run function in the IDE (usually a green triangle in PyCharm and VS Code). You should see output in the terminal similar to the following:
-
Click on the URL which is likely to be the default http://127.0.0.1:5000. You should see your app in a browser.
The app has been run on a development server and hosted on your machine at that URL. You are accessing your app locally. It is not accessible by anyone else.
If another program is already using port 5000, you’ll see an OSError when the server tries to start. It may have one of the following messages:
OSError: [Errno 98] Address already in use OSError: [WinError 10013] An attempt was made to access a socket in a way forbidden by its access permissions
You can specify a different port number in the code that runs the app, e.g.:
if __name__ == '__main__': app.run(port=5001)
Another way to run the Flask app is to run it from the Terminal command line. This is the way that is recommended in the Flask documentation and will be used in all future activities.
Use the --app
option to point to your application, and the --debug
option to enable debug mode.
Use flask run --help
to see the available options.
flask --app flask_app1/app1.py run --debug
Add --port 5001
(or any other port number that is not in use) to the end of the command chain if you need to change
the port number.
Try entering the above command in Terminal.
What happened?
You already have a running Flask app from the earlier activity. You will need to stop that app from running before you try to start another on the same port.
If you ran the app in PyCharm you may have a button (red outlined square) in the interface that allows you to stop the app. If so, press this.
Otherwise, you will need to stop it from the command line interface (CLI) by pressing the ctrl
and c
keys at
the same time. If you look at the CLI in the image above you can see that you are told to do this.
Stop the running app using CTRL+C
.
Now go back to Terminal and start the app from the command line using flask --app flask_app1/app1.py run --debug
(
add --port 5001
to the end of the command chain if you need to change the port number).
You will not learn HTML and Jinja templates until after reading week. However, so you can see the difference, open the flask_app2 folder.
-
static
contains CSS and JavaScript files. The files in the folders are downloaded from Bootstrap. -
templates
contains two files. These have a parent/child relationship where the child inherits all the parent's content:layout.html
, the parent. A mix of HTML that defines the page layout, and Jinja that defines blocks, areas that child pages can implement.index.html
, the child. A mix of HTML and Jinja. The layout is inherited fromlayout.html
and then the Jinja blocks for the page title and content are specified for this page.
Run the app from the Terminal command line in your IDE. You should see the styling (e.g. font, page margins) looks different.
flask --app flask_app2/app2.py run --debug
Don't forget to stop the running app using CTRL+C
in the command line interface.
In the paralympics folder, create an appropriately named python file and add code to create a basic app with a home page message.
You can give the python file any name except for flask.py (calling it flask.py
would conflict with Flask itself.).
Challenge: try and add a variable route that allows the user to enter their name and a personalised homepage message e.g. 'Hello name and welcome to my paralympics app'.
See Flask documentation:
Make sure you can run and stop the paralympics app.
A solution will be given in next week's coding activities.
-
Read the coursework specification.
-
Sign in to GitHub.
-
Use the correct link (individual/group) to create the coursework repository from the GitHub classroom:
- Hyperlink: COMP0034 Coursework 2 (Individual)
- Accept the assignment.
- Accept to join the ucl-comp0035 organisation if prompted
- Hyperlink: COMP0034 Coursework 1 (Group)
- One person only (first person from the group)
- Accept the assignment.
- If prompted, accept to join the comp0035-ucl organisation.
- Create the group (team) using the same name as your Moodle group name (e.g. Group_01, Group_02 etc.).
- All other group members
- Accept the assignment.
- If prompted, accept to join the comp0035-ucl organisation.
- Select your group (team) name from the list.
- Hyperlink: COMP0034 Coursework 2 (Individual)
-
Find the repository you just created in the ucl-comp0035 organisation
-
Clone your repository to your IDE (e.g. VS Code, PyCharm).
-
Create and activate a virtual environment.
-
Install Flask in the virtual environment e.g.
pip install Flask
-
Create a README.md
-
Create a requirements.txt
-
Create an 'src' directory in the project files (or create a directory with your app's name if you prefer)
-
Create a pyproject.toml
-
Create a 'data' directory and add your data file(s) (.csv, .xlsx)
-
Create an appropriately named python file in the directory created at step 10. Add code to create a basic Flask app.
-
Commit and push the changes to GitHub.
-
Make sure your Flask app can run and be stopped.
Steps 2 - 11 were covered several times in COMP0035 so are no longer repeated in detail in this course. Refer to tutorials 1 and 2 in COMP0035.