I have the code below in my Python script:
def cmd_wui(argv, path_to_tx):
"""Run a web UI."""
from flask import Flask, flash, jsonify, render_template, request
import webbrowser
app = Flask(__name__)
@app.route('/tx/index/')
def index():
"""Load start page where you select your project folder
or load history projects from local DB."""
from txclib import get_version
txc_version = get_version()
prj = project.Project(path_to_tx)
# Let's create a resource list from our config file
res_list = []
prev_proj = ''
for idx, res in enumerate(prj.get_resource_list()):
hostname = prj.get_resource_host(res)
username, password = prj.getset_host_credentials(hostname)
return render_template('init.html', txc_version=txc_version, username=username)
Also, I have an HTML form in init.html:
<form>
<input type="text" id="projectFilepath" size="40" placeholder="Spot your project files">
<input type="button" id="spotButton" value="Spot">
</form>
How can I pass the user input from “projectFilepath” when a user clicks “spotButton” on a variable in my python script?
I’m new in Python and Flask, so forgive me if I make any mistakes.
The
formtag needs some attributes set:action: The URL that the form data is sent to on submit. Generate it withurl_for. It can be omitted if the same URL handles showing the form and processing the data.method="post": Submits the data as form data with the POST method. If not given, or explicitly set toget, the data is submitted in the query string (request.args) with the GET method instead.enctype="multipart/form-data": When the form contains file inputs, it must have this encoding set, otherwise the files will not be uploaded and Flask won’t see them.The
inputtag needs anameparameter.Add a view to handle the submitted data, which is in
request.formunder the same key as the input’sname. Any file inputs will be inrequest.files.Set the form’s
actionto that view’s URL usingurl_for: