Run Python Method In Html Web Page
I have a HTML page with a button. This web page is running on Flask. What I want to do is when the user press a button, I want to invoke Python method. main.py from flask import u
Solution 1:
this can not be done.
because the 'click' happend in browser, but 'first' run on your server.
what you need is a html form or ajax request.
Solution 2:
I found the way to run python script (method) on flask, and it's really easy. Just import python file in the flask function. There might be some limitations if you are developing a page with number of python classes, but this solution solved my problem.
from flask import Flask, render_template
app = Flask(__name__)
@app.route('/')defindex(name=None):
return render_template('tour.html',name=name)
@app.route('/exec')defparse(name=None):
import Parse
print("done")
return render_template('tour.html',name=name)
if __name__ == '__main__':
app.run(host='0.0.0.0')
app.debug = True
tour.html
<a href="/exec"> This link will invoke Parse method </a>
This will run "Parse.py" on click of button.
It worked totally fine on local , and now i'm going to test on heroku server. Will there be any problems in using this way?
Post a Comment for "Run Python Method In Html Web Page"