简体   繁体   中英

Run python script from Flask server

I need to be able to run a Python script from my HTML file on my Flask server. On my HTML page, there is 3 input boxes, and an ok button. When the ok button is pressed, it should pass the number in the input boxes into a command to run a python script. An example of the command that would need to be run is python magicHome-rgb.py <inputBox1> <inputBox2> <inputBox3> , but replacing the <inputBox[no]> with the input from the input box.

If this doesn't make sense, feel free to ask more...

Does anyone know if this is possible?

No, and even if you could, don't. Instead, if you are using forms to gather your data, send that data to your Flask server with a POST request and have a function handle it. Don't use a separate script.

Example python function:

@app.route('/your-url', methods=('GET', 'POST'))
    def get_input():
        if request.method == 'POST':
            box1 = request.form['inputbox1']
            box2 = request.form['inputbox2']
            box3 = request.form['inputbox3']

            # -- do stuff --

        return render_template('your_template.html')

HTML example of a form that might be used:

  <form method="post">
    <input type=number name="inputbox1" id="inputbox1" required/>
    <input type=number name="inputbox2" id="inputbox2" required/>
    <input type=number name="inputbox3" id="inputbox3" required/>
    <input type="submit" value="Ok">
  </form>

In the above case, box1 , box2 , and box3 store the outputs of the 3 input boxes that you might need.

Alternatively, to run code from the HTML file, you could also process the input with javascript embedded in the HTML file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM