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.

2
  • What kind of http server do you use? Commented Feb 15, 2017 at 23:59
  • Include more in your post (code) and maybe you want a framework Commented Feb 16, 2017 at 0:05

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.