Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sending data from HTML form to a Python script in Flask

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.

like image 550
Mpampinos Holmens Avatar asked Jul 19 '12 08:07

Mpampinos Holmens


People also ask

How do I pass data from HTML form to Python file?

in the form action form action="" , put the location of your cgi script and the value of the textbox will be passed to the cgi script. eg. thus you will get the key word entered in search text box in searchterm variable in python.

How do I submit form data in Flask?

In the <form> tag, you set the method attribute to post so the form data gets sent to the server as a POST request. In the form, you have a text input field named title ; this is the name you'll use on the application to access the title form data. You give the <input> tag a value of {{ request.


2 Answers

The form tag needs some attributes set:

  1. action: The URL that the form data is sent to on submit. Generate it with url_for. It can be omitted if the same URL handles showing the form and processing the data.
  2. method="post": Submits the data as form data with the POST method. If not given, or explicitly set to get, the data is submitted in the query string (request.args) with the GET method instead.
  3. 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 input tag needs a name parameter.

Add a view to handle the submitted data, which is in request.form under the same key as the input's name. Any file inputs will be in request.files.

@app.route('/handle_data', methods=['POST']) def handle_data():     projectpath = request.form['projectFilepath']     # your code     # return a response 

Set the form's action to that view's URL using url_for:

<form action="{{ url_for('handle_data') }}" method="post">     <input type="text" name="projectFilepath">     <input type="submit"> </form> 
like image 84
codecool Avatar answered Oct 09 '22 14:10

codecool


You need a Flask view that will receive POST data and an HTML form that will send it.

from flask import request  @app.route('/addRegion', methods=['POST']) def addRegion():     ...     return (request.form['projectFilePath']) 
<form action="{{ url_for('addRegion') }}" method="post">     Project file path: <input type="text" name="projectFilePath"><br>     <input type="submit" value="Submit"> </form> 
like image 34
Dawid Avatar answered Oct 09 '22 15:10

Dawid