Background:
I have created a very simple front end, where the user can input strings. Once entered and "check" button is clicked, I would like to pass this string as a JSON to a python string where it will do a SQL look up. Based on the SQL look the python script should pass a boolean value which should change the ? to a ✔ or a ✘.
Question:
How can I pass on a string once the "check' button is pressed as a JSON to a Python script, and pass a Boolean from Python to HTML to change the ? to a ✔ or a ✘?
Research:
body {font-family: Arial, Helvetica, sans-serif;}
* {box-sizing: border-box;}
input[type=text], select, textarea {
width: 100%;
padding: 12px;
border: 1px solid #ccc;
border-radius: 4px;
box-sizing: border-box;
margin-top: 6px;
margin-bottom: 16px;
resize: vertical;
}
input[type=submit] {
background-color: #4CAF50;
color: white;
padding: 12px 20px;
border: none;
border-radius: 4px;
cursor: pointer;
}
input[type=submit]:hover {
background-color: #45a049;
}
.container {
border-radius: 5px;
background-color: #f2f2f2;
padding: 20px;
}
h3 {text-align: center;}
.center {
display: flex;
justify-content: center;
align-items: center;
}
My Request
? Account Name:
? Contact Name:
? Reseller:
? Issue Date:
解决方案
This is just a sample code, which will make you understand how to pass values from client to server as well as server to client.
Asumption: 'Flask' is you current working directory
Please follow the below steps:
Install Flask
Run the below command
pip install Flask
Create a python file app.py, copy paste the below content into this file.
from flask import Flask, render_template, request
app = Flask(__name__)
@app.route('/')
def index():
return render_template('index.html')
@app.route('/greet', methods=['POST'])
def greet():
name = request.form['name']
return render_template('greet.html', name=name)
if __name__ == '__main__':
app.run()
Create a file index.html in location '/Flasak/templates' as
Welcome
Name: Submit
Create a file greet.html in location '/Flasak/templates' as
Have a good day, {{name}}
Run the python file like below
python app.py
Open a browser and hit http://localhost:5000, it will display 'welcome', along with a field to input name. Provide your name and hit Submit. Like this you can send the values to server from client.
After pressing Submit, redirection will happen, the server will receive name and send it again to client. Now you should be able to see Have a good day, along with the name provided.
Just for your reference the project's directory will be looking like below:
Flask
|
|-> templates
| |
| |-> greet.html
| |-> index.html
|
|-> app.py
Note: For better understanding, I would recommend you to go through tutorials. I hope this was helpful to you.