【问题标题】:How to run a python script from html如何从 html 运行 python 脚本
【发布时间】:2017-02-15 23:57:20
【问题描述】:

我一直在尝试通过 HTML 按钮运行 python 脚本 (rainbow.py)。我不知道该怎么做,我找到的所有答案对我来说毫无意义。我想要的只是简单解释一下如何通过常规 HTML 脚本运行 python 脚本。

我正在尝试在 Raspberry Pi 上执行此操作。

【问题讨论】:

  • 你使用什么样的http服务器?
  • 在您的帖子(代码)中包含更多内容,也许您想要framework

标签: html python-3.x raspberry-pi


【解决方案1】:

您可以通过Flask,一个Python Web 微框架

HTML (index.html):

<a href="{{ url_for('do_something') }}">Your button</a>

Python (app.py):

from flask import Flask, render_template

app = Flask(__name__)

@app.route('/')
def index():
    # render your html template
    return render_template('index.html')

@app.route('/something')
def do_something():
    # when the button was clicked, 
    # the code below will be execute.
    print 'do something here'
    ...

if __name__ == '__main__':
    app.run()

启动服务器:$ python app.py

然后转到http://127.0.0.1:5000

文件结构:

template\  
    -index.html  
app.py

【讨论】:

    猜你喜欢
    • 2021-04-11
    • 2021-10-16
    • 2021-03-11
    • 2017-04-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-10
    • 2017-05-18
    相关资源
    最近更新 更多