Learn to create an advanced web server with Flask in Raspberry Pi. Create a web server using the Raspberry Pi and Python Code. You can also include GPIO Pins in the webserver. Learn about building a web server with dynamic content and run it locally on Raspberry Pi.
Steps for creating an advanced web server with Flask in Raspberry Pi.
Step 1: Install Flask in Raspberry Pi
Start the Terminal window in Raspberry Pi and install Flask by entering below given command
sudo apt-get install python3-flask
Step 2: Building Advanced web server with dynamic content
- Create a new folder for web application
- Create a new folder named “templates” inside the main folder
- Make a new template in “templates” folder of HTML file with name “name.html”
<html> <body> <h1>Hello from {{ name }}</h1> </body> </html>
- Open Python 3
- Create a new file and write the below-given sample code in it
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersfrom flask import Flask, render_template app = Flask(__name__) @app.route('/') def index(): return 'Hello world' @app.route('/<name>') def hello(name): return render_template('name.html', name=name) if __name__ == '__main__': app.run(debug=True, host='0.0.0.0') - Run the code with a python shell. Learn information about running the python code.
- Open the web browser and navigate to below-given URL to open the web application
Learn the basics of creating a sample web server using Flask in Raspberry Pi.
Learn basic hardware information of Raspberry Pi 3.