Запустите скрипт Python из HTML-страницы Flask - PullRequest
0 голосов
/ 06 декабря 2018

Как мы выполняем или запускаем код Python после нажатия кнопки отправки в html-форме, которую я создал в index.html.

Ниже приведен код index.php:

<!DOCTYPE html>
   <head>
      <title>Hello</title>
   </head>

   <body>
      <form action="" method="POST">
        <input type="text" name="nopol">
        <input id="print_nopol" type="submit">
      </form>

     <?php
        system("python /home/pi/python-epson-printer/epson_printer/testpage.py");
     ?>
   </body>
</html>

и ниже - это скрипт приложения фляги Python с именем app.py:

from flask import Flask, request, render_template, Response, redirect, url_for
from os import listdir
app = Flask(__name__)

@app.route('/')
def my_form():
    return render_template('index.html')

@app.route('/', methods=['POST'])
def my_form_post():
    input_nopol = request.form['nopol']
    if request.method == 'POST':
       with open('nopol.txt', 'w') as f:
            f.write(str(input_nopol))
    return render_template('index.html', nopol=input_nopol)

if __name__ == "__main__":
   app.run(host='192.168.1.2', port=8080, debug=True)

Этот код Python имеет функцию для вставки данных в файл .txt, а вот путь к скрипту Python:

/home/pi/server_print/print.py

Спасибо за вашу помощь.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...