-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?