Issue
I am working on a UI where I am supposed to run a python script with a HTML button. I am using flask for it.
There are two "insight engine" buttons in my UI. One in navigation bar at top and another one is simple button in blue color with upload icon.
Below is the code:
- for flask main.py
from flask import Flask, render_template
from flask_bootstrap import Bootstrap
from flask_nav import Nav
from flask_nav.elements import *
from dominate.tags import img
import config
from doc_parser import get_text_from_pdf
topbar = Navbar(
View('Reports', 'get_reports'),
View('Insight Engine', 'get_engine')
)
nav = Nav()
nav.register_element('top', topbar)
app = Flask(__name__)
Bootstrap(app)
@app.route('/', methods=['GET'])
def index():
return(render_template('index.html'))
@app.route('/reports', methods=["GET"])
def get_reports():
return(render_template('reports.html'))
@app.route('/engine', methods=["GET"])
def get_engine():
get_text_from_pdf(config.doc_path) #Here I am calling python function
return(render_template('engine.html'))
nav.init_app(app)
if __name__ == '__main__':
app.run(debug=True)
- for insight engine button (blue one):
<a href="/engine" class="upload1" style="padding: 0.8%; border-radius: 5px; width: 12%;"><i class="fa fa-upload"></i><span style="display:inline-block; width: 5px;"></span> Insight Engine</a>
Question: Either I click on the top button or blue button, both are running my python script. I know it is because I am giving same route to both. But What I want is that when I click top button , it will go to insight engine nav tab without running python script. But when I click blue button, it will run the python script and then it will go to insight engine nav tab.
In simple terms, I want to go to same route (/engine) with two diff functions. One which will not run my python script and one which will run my python script.
How to do this?
Solution
If you want to use the same route you need to pass somehow information should it run script or not. You can achieve that using query string.
@app.route("/engine", methods=["GET"])
def get_engine():
use_script = request.args.get("use_script")
if use_script:
get_text_from_pdf(config.doc_path) # Here I am calling python function
return render_template("engine.html")
And the blue button:
<a href="/engine?use_script=1" class="upload1" style="padding: 0.8%; border-radius: 5px; width: 12%;"><i class="fa fa-upload"></i><span style="display:inline-block; width: 5px;"></span> Insight Engine</a>
Other option could be use different route in button, where you run the script and return redirect to /engine
Answered By - kosciej16
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.