Python Forum
Connecting python script into HTML with flask
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Connecting python script into HTML with flask
#1
Hello,

I'm currently working on a project and I need some help with flask since I'm a bit new to flask I dont know how to connect my python script into html.

Say, I have input_data.html and input_data.py how do I connect them with flask? When user input the sentence and click the button, my script (input_data.py) will process the input and display the output on text area below.

[Image: unknown.png]

Any help or guidance would be appreciated.
Reply
#2
There are couple or more ways do do this.
when push Submit button on server request.form will get the text from input field.
Can send text back(redirect) to same page.
A other way is send back to same page with Ajax(that dos not reload the page).
A basic example.
app.py:
from flask import Flask, render_template, request, redirect, url_for
 
app = Flask(__name__)
@app.route('/')
def index():
   return render_template("index.html")

@app.route('/text', methods=['GET', 'POST'])
def text(comments=[]):
    if request.method == "GET":
        return render_template("index.html", comments=comments)    
    comments.append(request.form["text_input"])  
    return redirect(url_for('text'))

if __name__ == '__main__':
   app.run(debug=True)
index.html:
<!doctype html>
<html>
<head>
  <title>Some title</title>
  <link rel=stylesheet type=text/css href="{{ url_for('static', filename='css/style.css') }}" />
</head>
<body>
  <h2>Form</h2>
  <form action="/text" method="post">
    Input text:<br>
    <input type="text" name="text_input" value="">
    <br>
    <input type="submit" value="Submit">
  </form>
  <div id='foo'>
    <br>
    {% for comment in comments %}
    <div class="row">
      {{ comment }} 
    </div>
    {% endfor %}
  </div>
</body>
</html>
So to server and send back to view.
A common way is to use jinja2 build into Flask to sent stuff to html {{ comment }}.
[Image: RmWrDq.jpg]
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Flask - use bootstrap styles in HTML Krayna 1 1,046 Aug-29-2023, 02:33 PM
Last Post: menator01
  show csv file in flask template.html rr28rizal 8 34,716 Apr-12-2021, 09:24 AM
Last Post: adamabusamra
  HTML multi select HTML listbox with Flask/Python rfeyer 0 4,617 Mar-14-2021, 12:23 PM
Last Post: rfeyer
  gettin flask to call script periodically GrahamL 1 2,209 Jan-08-2021, 06:11 PM
Last Post: ndc85430
  API auto-refresh on HTML page using Flask toc 2 11,835 Dec-23-2020, 02:00 PM
Last Post: toc
  python 3.7 on windows using flask and flask-sqlalchemy. Alpy 2 3,983 Aug-12-2020, 07:24 PM
Last Post: Alpy
  Flask formatting into HTML engineer_geek 1 2,775 Aug-05-2020, 01:51 AM
Last Post: engineer_geek
  Python3 + BeautifulSoup4 + lxml (HTML -> CSV) - How to loop to next HTML/new CSV Row BrandonKastning 0 2,353 Mar-22-2020, 06:10 AM
Last Post: BrandonKastning
  [Flask] html error 405 SheeppOSU 0 2,343 Jun-08-2019, 04:42 PM
Last Post: SheeppOSU
  how i save the html form to flask database mebaysan 1 7,285 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