Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Using python within an html form
#1
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?
Reply
#2
https://stackoverflow.com/questions/1596...hon-script
Reply
#3
(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?
Reply
#4
(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.
Reply
#5
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.
Reply
#6
(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!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Post HTML Form Data to API Endpoints Dexty 0 1,382 Nov-11-2021, 10:51 PM
Last Post: Dexty
  HTML multi select HTML listbox with Flask/Python rfeyer 0 4,530 Mar-14-2021, 12:23 PM
Last Post: rfeyer
  Using Python request without selenium on html form with javascript onclick submit but eraosa 0 3,135 Jan-09-2021, 06:08 PM
Last Post: eraosa
  how does a html form work exactly? mp3909 2 2,058 Apr-01-2020, 04:02 PM
Last Post: mp3909
  Python3 + BeautifulSoup4 + lxml (HTML -> CSV) - How to loop to next HTML/new CSV Row BrandonKastning 0 2,329 Mar-22-2020, 06:10 AM
Last Post: BrandonKastning
  Posting value from excel to Form (Python+Selenium) revanth 0 1,764 Feb-05-2020, 10:44 AM
Last Post: revanth
  requests post/get to HTML form mrdominikku 1 2,299 Nov-03-2019, 07:12 PM
Last Post: Larz60+
  getting options from a html form pgoosen 5 3,193 Jul-03-2019, 06:07 PM
Last Post: nilamo
  How to send data from remotely hosted HTML form to Pi sajid 2 2,530 Jun-27-2019, 10:28 PM
Last Post: sajid
  how i save the html form to flask database mebaysan 1 7,244 Feb-07-2019, 12:56 AM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020