Build a Python Web Server with Flask: Difference between revisions

From Sensors in Schools
Jump to navigation Jump to search
No edit summary
No edit summary
Line 57: Line 57:


[[File:Screenshot 2023-07-22 at 8.07.01 am.png | 900px]]
[[File:Screenshot 2023-07-22 at 8.07.01 am.png | 900px]]
= Add a New Page =
* Now we are going to add a new page to your web app by creating a new '''route'''.
* In web applications, a route is a specific path into a website.
* It is determined by the URL.
* Each route can lead to a different page in a web site.
In the code you already have in app.py, there is a single route:
@app.route('/')
def index():
    return 'Hello world'
This route is made up of three parts:
@app.route('/'): this determines the entry point; the / means the root of the website, so https://127.0.0.1:5000/
def index(): this is the name you give the route, in this case index, because it’s the index (or homepage) of the website
return 'Hello world': this is the content of the web page, which is returned when the user goes to this URL
The second half of the app.py code runs the web server and your app:

Revision as of 22:10, 21 July 2023

Build a Python Web Server using Flask

Install Flask

  • Open the Terminal
  • Enter the command pip3 install flask

  • Make a new directory named webapp using the Terminal command mkdir webapp
  • Enter the new directory with the command cd webapp

First web application =

  • Open up Thonny.
  • Paste the following code into Thonny.
  • Save the file as app.py in the webapp directory.

from flask import Flask

app = Flask(__name__)

@app.route('/')
def index():
    return 'Hello world'

if __name__ == '__main__':
    app.run(debug=True, host='0.0.0.0')

Run Flask from Terminal

  • Open the Terminal
  • To run the app.py program enter the command python3 app.py from within the /home/pi/webapp directory
  • You should see a similar response.

  • Now open your web browser and enter the IP address of your Raspberry Pi.
  • You can find the IP address of your Pi by hovering your mouse over the WiFi symbol.
    • Or by entering the URL localhost in the browser search bar.
    • Or entering the command ifconfig in a new Terminal window.
  • You should see this response.

Local Network Web site access

  • Any computer on the same local network can access your Flask web server.
  • On my local network the IP address of the Raspberry Pi is 192.168.1.118
  • My Mac computer can also access the Flash web server running on the Raspberry Pi by entering the IP address of the Pi in a web browser.
  • The result is shown below.

Add a New Page

  • Now we are going to add a new page to your web app by creating a new route.
  • In web applications, a route is a specific path into a website.
  • It is determined by the URL.
  • Each route can lead to a different page in a web site.

In the code you already have in app.py, there is a single route:

@app.route('/') def index():

   return 'Hello world'

This route is made up of three parts:

@app.route('/'): this determines the entry point; the / means the root of the website, so https://127.0.0.1:5000/ def index(): this is the name you give the route, in this case index, because it’s the index (or homepage) of the website return 'Hello world': this is the content of the web page, which is returned when the user goes to this URL The second half of the app.py code runs the web server and your app: