Jaeseok An
Jaeseok An

Reputation: 58

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 url_for, Flask, render_template

app = Flask(__name__)

@app.route('/')
def hello_world():
    return render_template('tour.html')

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

def first():
    global todayDate
    parseWemake(*Wemake.wemakeData())
    parseCoupang(*Coupang.coupangData())
    parseTmon(*Tmon.tmonData())

tour.html

<button>Click</button>

I am now lost here. All I want to do is invoke first() method on click that button on html.

Thanks in advance.

Upvotes: 0

Views: 2284

Answers (2)

Jaeseok An
Jaeseok An

Reputation: 58

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('/')
def index(name=None):
    return render_template('tour.html',name=name)

@app.route('/exec')
def parse(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?

Upvotes: 1

Yuan Wang
Yuan Wang

Reputation: 155

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.

Upvotes: 1

Related Questions