You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: worksheet.md
+11-48Lines changed: 11 additions & 48 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -2,67 +2,31 @@
2
2
3
3
Install the lightweight web framework Flask and set up a basic web server with different pages, using Python, HTML, and CSS.
4
4
5
-
## Installing pip and Flask
5
+
## Installing Flask
6
6
7
7
First you're going to install the Flask package. Make sure you are connected to the internet, either by Ethernet cable or WiFi before you start.
8
8
9
9
1. Start by opening a Terminal window from the taskbar or applications menu:
10
10
11
11

12
12
13
-
1.Next, you'll need to install `pip` by typing the following command and pressing **Enter** on the keyboard:
13
+
1.Now install Flask by typing:
14
14
15
15
```bash
16
-
sudo apt-get install python-pip
16
+
sudo apt-get install python3-flask
17
17
```
18
-
19
-
`pip` is a tool for installing Python packages from the Python Packaging Index (PyPi): you can browse packages at [pypi.python.org](https://pypi.python.org/).
20
-
21
-
1. Now use `pip` to install Flask by typing:
22
-
23
-
```bash
24
-
sudo pip install flask
25
-
```
26
-
27
-
And press **enter**.
28
18
29
19
## Building a basic Flask web application
30
20
31
21
Now you're going to set up a basic web application with Flask and Python. You will be able to run a single web page and display some text on a web browser.
32
22
33
-
1. Return to the Terminal window and create a new folder called `webapp` by typing:
23
+
1. Open the **File Manager** and create a new folder for your project.
34
24
35
-
```bash
36
-
mkdir webapp
37
-
```
38
-
39
-
`mkdir` means "make directory": a directory is a folder which can contain files and more folders.
25
+
1. Open Python 3 from the main menu.
40
26
41
-
1. Navigate into this directory by using the `cd` command and pressing **Enter**:
42
-
43
-
```bash
44
-
cd webapp
45
-
```
46
-
47
-
`cd` means "change directory": you use it to enter a folder.
48
-
49
-
1. Flask applications can be run from a single file, so you need to create the file now using the `touch` command, like this:
50
-
51
-
```bash
52
-
touch app.py
53
-
```
54
-
55
-
This will create a file named `app.py`, in which all our application code will be written.
56
-
57
-
1. Enter the following command to open this file in the **Python 2** IDE (IDLE), in order to get started writing your web app:
58
-
59
-
```bash
60
-
idle app.py &
61
-
```
62
-
63
-
The ampersand (&) on the end of this command tells it to open IDLE in a new process. Unlike a command like `cd`, this command doesn't finish until you close the IDLE window. Opening IDLE in a new process allows you to enter more commands into the Terminal without quitting IDLE.
27
+
1. Open a new window by clicking `File > New file`, and save this as `app.py` inside the project folder you created.
64
28
65
-
1. An empty window with `app.py` displayed in the title bar will appear. You'll write your application code here and when you run your code, any printed messages or errors will be shown in a Python shell window.
29
+
1. You'll write your application code here and when you run your code, any printed messages or errors will be shown inthe Python shell window which opened first.
66
30
67
31
1. Now enter the following lines into the blank `app.py` window:
68
32
@@ -81,14 +45,13 @@ Now you're going to set up a basic web application with Flask and Python. You wi
81
45
82
46
Note here the `host='0.0.0.0'` means the web app will be accessible to any device on the network.
83
47
84
-
1. Save the file with `Ctrl + S`. Now return to the Terminal window and enter `python app.py` to run the web server.
48
+
1. Save the file with `Ctrl + S`. Now return to the Terminal window and enter `python3 app.py` to run the web server.
85
49
86
50
If everything has been written correctly, you should see an output similar to this:
87
51
88
52
```
89
-
* Restarting with stat
90
-
* Debugger is active!
91
-
* Debugger pin code: 425-241-775
53
+
* Running on http://0.0.0.0:5000/
54
+
* Restarting with reloader
92
55
```
93
56
94
57
1. Open the Pi's web browser from the taskbar or application menu and navigate to `http://127.0.0.1:5000/`. You should see a white screen with the words `Hello world`:
@@ -173,7 +136,7 @@ Next, you'll modify your existing routes to return full HTML templates, rather t
173
136
174
137
Flask will look for`index.html`in a directory called `templates`, in the same directory as the `app.py` file.
175
138
176
-
1. Save the file. Make sure your web app is still running. If you stopped it, just run `python app.py` from your `webapp` directory.
139
+
1. Save the file. Make sure your web app is still running. If you stopped it, just run `python3 app.py` from your `webapp` directory.
177
140
178
141
1. Reload the route in your web browser (go to the base route at `http://127.0.0.1:5000/`) to see your new HTML template being displayed.
0 commit comments