0

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.

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.