0

I have been trying to run a python script (rainbow.py) through a HTML button. I have no idea how to do this and all the answers I have found make no sense to me. All I want is a simple explanation of how to run a python script through regular HTML script.

I am trying to do this on a Raspberry Pi.

Anna Pawlicka
  • 757
  • 7
  • 22
Jacob
  • 1
  • 1
  • 1

1 Answers1

2

You can make it through Flask, a Python Web Microframework:

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()

Start the server:$ python app.py

Then go to http://127.0.0.1:5000.

File structure:

template\  
    -index.html  
app.py
Grey Li
  • 11,664
  • 4
  • 54
  • 64