Raspbian (Bullseye) on RPi 3B+: Using HTML Forms to Post/Get Data with Python Script and Flask
Raspbian (Bullseye) is the latest version of the official Raspberry Pi operating system. It is based on Debian 11 and provides a stable and secure platform for Raspberry Pi 3B+ users. In this article, we will explore how to use HTML forms to post and get data using a Python script and Flask on Raspbian (Bullseye) on RPi 3B+.
HTML Forms
HTML forms are used to collect user input and send it to a server for processing. The user input can be sent to the server using the GET or POST method. In this article, we will focus on using the POST method to send data to the server.
Python Script and Flask
Flask is a micro web framework for Python. It is lightweight, easy to use, and provides a simple way to create web applications. In this article, we will use Flask to create a simple web application that can receive data from an HTML form and process it using a Python script.
Setting up the Environment
Before we can start, we need to install Flask and set up a new project. To install Flask, open a terminal and run the following command:
sudo apt-get install python3-flask
Next, create a new directory for your project and navigate to it in the terminal.
Creating the HTML Form
Create a new file called index.html in your project directory and add the following code:
<!DOCTYPE html>
<html>
<head>
<title>Raspbian (Bullseye) on RPi 3B+: Using HTML Forms to Post/Get Data with Python Script and Flask</title>
</head>
<body>
<form action="/submit" method="post">
<label for="name">Name:</label>
<input type="text" id="name" name="name">
<br>
<label for="email">Email:</label>
<input type="email" id="email" name="email">
<br>
<input type="submit" value="Submit">
</form>
</body>
</html>
This code creates a simple HTML form with two fields: name and email. When the form is submitted, the data is sent to the /submit endpoint on the server using the POST method.
Creating the Python Script and Flask App
Create a new file called app.py in your project directory and add the following code:
from flask import Flask, request, render\_template
app = Flask(<name>)
@app.route('/')
def index():
return render\_template('index.html')
@app.route('/submit', methods=['POST'])
def submit():
name = request.form['name']
email = request.form['email']
print(f'Name: {name}')
print(f'Email: {email}')
return 'Success!'
if <name> == '<main>':
app.run(host='0.0.0.0', port=80)
This code creates a new Flask app and defines two routes. The first route, /, renders the index.html template. The second route, /submit, is called when the form is submitted. It retrieves the name and email fields from the form data and prints them to the console. Finally, the app is run on port 80.
Testing the App
To test the app, run the app.py script in the terminal. Once the app is running, open a web browser and navigate to http://<your\_ip\_address>. You should see the HTML form. Enter your name and email and click the 'Submit' button. The data should be printed to the console.
In this article, we have explored how to use HTML forms to post and get data using a Python script and Flask on Raspbian (Bullseye) on RPi 3B+. We have covered the basics of HTML forms, Python scripting, and Flask. With this knowledge, you can create your own web applications on Raspbian (Bullseye) on RPi 3B+.
References
- Flask documentation: https://flask.palletsprojects.com/en/2.1.x/
- Raspbian (Bullseye) documentation: https://www.raspbian.org/RaspbianBullseye
- Raspberry Pi 3B+ documentation: https://www.raspberrypi.org/products/raspberry-pi-3-model-b-plus/