Python Forum

Full Version: Using python within an html form
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi everyone,
I'm wondering if what I'm trying to do is possible. Something that would normally be done with jquery, but I'm wondering if I can simplify using python.

Say I have an html form, with a field and submit button. On click, I would like to take the value input by user, query a db using that value (probably with pyodbc), and then return to that user the result from the query.

I would like to do this using python and flask as opposed to php/javascript/jquery.

Is something like this possible with python?
(Aug-14-2020, 07:57 PM)t4keheart Wrote: [ -> ]Say I have an html form, with a field and submit button. On click, I would like to take the value input by user, query a db using that value (probably with pyodbc), and then return to that user the result from the query.

Are you doing some client-side validation or making AJAX requests to the server with JQuery? You're of course still going to need JavaScript on the client for those, as I already mentioned in this thread. Of course you can do the server-side work (receiving the request, talking to the DB and returning the response) in Python with whatever libraries you want. Is this actually the same question as in your other thread?
(Aug-14-2020, 07:57 PM)t4keheart Wrote: [ -> ]Is something like this possible with python?
Of course this is possible,did't we answer this in the other thread?

The only way to learn this is to try stuff out,both Flask and Django comes build in web-server for local development this make it easy to start.
Quick demo that may help you get started,take first hit on Google about html form,then send it to a Flask route instead to /action_page.php.

Folder/files
simple\
  |-- app.py
templates\
  |-- index.html 
index.html
<!DOCTYPE html>
<html>
<body>

<h2>HTML Forms</h2>

<form action="/my_form" method="post">
  <label for="fname">First name:</label><br>
  <input type="text" id="fname" name="fname" value="John"><br>
  <label for="lname">Last name:</label><br>
  <input type="text" id="lname" name="lname" value="Doe"><br><br>
  <input type="submit" value="Submit">
</form> 

<p>If you click the "Submit" button, the form-data will be sent to a Flask route called my_form".</p>

</body>
</html>
app.py
from flask import Flask, request, render_template, url_for

app = Flask(__name__)

@app.route("/")
def index():
    return render_template("index.html")

@app.route("/my_form", methods=["POST"])
def bar():
    f_name = request.form["fname"]
    l_name = request.form["lname"]
    full_name = f'<h2>First name: {f_name}<br>Last name: {l_name}</h2>'
    return full_name

if __name__ == "__main__":
    app.run(debug=True)
To run this,in folder of simple from command line.
G:\all_flask\2020\div\simple
λ python app.py
 * Serving Flask app "app" (lazy loading)
 * Environment: production
   WARNING: This is a development server. Do not use it in a production deployment.
   Use a production WSGI server instead.
 * Debug mode: on
 * Restarting with stat
 * Debugger is active!
 * Debugger PIN: 113-254-199
 * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
See that get a local host address,open that address in browser.

(Aug-14-2020, 08:44 PM)Axel_Erfurt Wrote: [ -> ]https://stackoverflow.com/questions/1596...hon-script
Not the best link Axel_Erfurt as it use CGI,this stuff is dead for Python web-development.
If fact the CGI module is deprecated in Python 3.8 and will be removed in 3.10.
Apparently there's Brython if you did want to write Python for the client. I don't know how efficient, mature, etc. this is though.
(Aug-15-2020, 05:40 AM)ndc85430 Wrote: [ -> ]
(Aug-14-2020, 07:57 PM)t4keheart Wrote: [ -> ]Say I have an html form, with a field and submit button. On click, I would like to take the value input by user, query a db using that value (probably with pyodbc), and then return to that user the result from the query.

Are you doing some client-side validation or making AJAX requests to the server with JQuery? You're of course still going to need JavaScript on the client for those, as I already mentioned in this thread. Of course you can do the server-side work (receiving the request, talking to the DB and returning the response) in Python with whatever libraries you want. Is this actually the same question as in your other thread?

You're right. It's the same project I'm talking about in both threads. I rephrased the question and compartmentalized the issue in my head because I'm stuck, but didn't realize I'm really just asking the same thing twice. My mistake.

(Aug-16-2020, 08:58 AM)snippsat Wrote: [ -> ]
(Aug-14-2020, 07:57 PM)t4keheart Wrote: [ -> ]Is something like this possible with python?
Of course this is possible,did't we answer this in the other thread?

The only way to learn this is to try stuff out,both Flask and Django comes build in web-server for local development this make it easy to start.
Quick demo that may help you get started,take first hit on Google about html form,then send it to a Flask route instead to /action_page.php.

Folder/files
simple\
  |-- app.py
templates\
  |-- index.html 
index.html
<!DOCTYPE html>
<html>
<body>

<h2>HTML Forms</h2>

<form action="/my_form" method="post">
  <label for="fname">First name:</label><br>
  <input type="text" id="fname" name="fname" value="John"><br>
  <label for="lname">Last name:</label><br>
  <input type="text" id="lname" name="lname" value="Doe"><br><br>
  <input type="submit" value="Submit">
</form> 

<p>If you click the "Submit" button, the form-data will be sent to a Flask route called my_form".</p>

</body>
</html>
app.py
from flask import Flask, request, render_template, url_for

app = Flask(__name__)

@app.route("/")
def index():
    return render_template("index.html")

@app.route("/my_form", methods=["POST"])
def bar():
    f_name = request.form["fname"]
    l_name = request.form["lname"]
    full_name = f'<h2>First name: {f_name}<br>Last name: {l_name}</h2>'
    return full_name

if __name__ == "__main__":
    app.run(debug=True)
To run this,in folder of simple from command line.
G:\all_flask\2020\div\simple
λ python app.py
 * Serving Flask app "app" (lazy loading)
 * Environment: production
   WARNING: This is a development server. Do not use it in a production deployment.
   Use a production WSGI server instead.
 * Debug mode: on
 * Restarting with stat
 * Debugger is active!
 * Debugger PIN: 113-254-199
 * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
See that get a local host address,open that address in browser.

(Aug-14-2020, 08:44 PM)Axel_Erfurt Wrote: [ -> ]https://stackoverflow.com/questions/1596...hon-script
Not the best link Axel_Erfurt as it use CGI,this stuff is dead for Python web-development.
If fact the CGI module is deprecated in Python 3.8 and will be removed in 3.10.

Thanks- and yes I didn't realize at the time that this is essentially the same question again. I suppose I didn't get the answer i wanted the first time around lol.

I'm familiar with setting up the barebones flask/django web server, very similar to express.js in functionality. I'm stuck on the portion of updating the form field with the click of the submit button (something usually done with AJAX?)

I think I'm going to give Brython a go. Sorry for the dublicate question!