Python Web Server with Flask Raspberry Pi Tutorial
By following this resource and setting up a Flask web server you will learn:
- How to install software on your Raspberry Pi
- How to install pip and Flask to create a Python-powered web server
- How to build a basic web app with Flask and run it as a local website on your Raspberry Pi
- How routes are used to map URLs to web pages
- How to use HTML to create simple web page templates
- How to use CSS to control the appearance of HTML content
- How to configure Flask and make your website accessible to other devices on your local network
1. Install Python 3 Flask
sudo apt-get install python3-flask
2. Make Application Directory for Flask Application
cd mkdir flaskapp cd flaskapp touch app.py
3. Copy and paste the content below into app.py
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')
Note here the host=’0.0.0.0′ means the web app will be accessible to any device on the network.
4. Run the Application
Save the file, and return to the Terminal window. Run the command
to start up the raspberry pi flask application.
If everything has been written correctly, you should see an output similar to this:
* Running on http://0.0.0.0:5000/
* Restarting with reloader
4. Head to 127.0.0.1:5000 in your Raspberry Pi Web Browser
You should now be able to navigate to 127.0.0.1:5000 (on your Raspberry Pi web browser) to see this!
Please comment below if you have any questions!
If you’d like to buy a Rasbperry Pi, check out some of the links below.