How to receive user data and return function value using flask?

Asked

Viewed 76 times

-1

I have a project where I would like to apply a python function using user-given information and return the function result to html. I know you normally use javascript for these cases, but my function uses some pandas functionality and it would be difficult to write the same code in javascript.

It turns out that I do not know how to access user data values or how to return the function result to html. What I tried was to use the remote:

<button action="{{ url_for('myfunction') }}">Mybutton</button> 

This worked for the function, but I can’t get the user value nor return the function result to html.

Here’s a replicable example of what I’ve tried so far:

python file

#app.py
from flask import Flask, render_template

app=Flask(__name__)

@app.route('/')
def render_index():
    return render_template('index.html', name='')

@app.route('/result')
def upper_name(name):
    return render_template('index.html', name=name.upper())

html file:

#templates/index.html
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <form>
        <label for="fname">First name:</label><br>
        <input type="text" id="fname" name="fname"><br>
        <p>Your upper case name is {{ name }}</p>
        <button action="{{ url_for('upper_name') }}">Upper</button> 
    </form>
</body>
</html>

Note that this example loads the page, allows the user to enter a name, but when the Upper button is applied, the page is not updated as desired.

How do I access user data values and return my function values to html using flask?

1 answer

2


Your "problem" is not right in Python but in HTML. It turns out that the attribute action does not exist in <button> (<button>: The Button element) and yes in <form>, then your form would look like this:

<!DOCTYPE html>
  ...
    <form action="{{ url_for('index') }}" method="post">
      <label for="fname">First name:</label><br>
      <input type="text" id="form_name" name="name"><br>
      <p>Your upper case name is {{ name }}</p>
      <button type="submit">Upper</button>
    </form>
  ...

See that the action was placed in <form> and <button> received the type "Submit" to indicate that it must submit the form when clicked. The addition of method="post" serves for the browser to use the HTTP method POST instead of GET to submit the form.

When you send by GET the data will be encoded in the URL itself, type "/? name=so", now imagine a complete form with name, phone, password etc...

But there is one thing that can be hit in Python since you don’t need to create two routes if at the end they will display the same content:

from flask import Flask, render_template, request

app = Flask(__name__)

@app.route('/', methods=['GET', 'POST'])
def index():

    if request.method == 'POST':
        name = request.form['name'].upper()
    else:
        name = ''

    return render_template('index.html', name=name)

Note that this route now accepts both the method GET as to the *POST.

When it is accessed via POST (i.e., upon receiving the form data) the "name" value is recovered, the method .upper() used to place the string uppercase and the "index.html" Rastered.

Otherwise, which is access via GET (the default mode), "index.html" is Rastered but using an empty value for "name".

Now a hint, you can put a condition in "index.html":

{% if name != "" %}
<p>Your upper case name is {{ name }}.</p>
{% endif %}

And so only display this message if there really is a name to be printed.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.