Assign a variable from HTML input to Python/Flask [duplicate] - python

This question already has answers here:
Get the data received in a Flask request
(23 answers)
Are global variables thread-safe in Flask? How do I share data between requests?
(4 answers)
Store large data or a service connection per Flask session
(1 answer)
Closed 3 years ago.
I have an app that uses a variable to to locate a row in a pandas dataframe. I want the user to use the input[type=range] in html to select a year that will be used as the variable x.
Here is my python file:
from flask import Flask, render_template, request
app = Flask(__name__)
x = '2005'
mask = (df['start'] < x) & (df['end'] > x)
output = df.loc[mask]
#app.route("/")
def home():
return render_template("index.html", output=output)
Here is my html form:
<form name="myform" method="POST">
<input type="range" name="yearInputName" id="yearInputId" value="1980" min="1880" max="2010">
</form>
How do I assign the variable x to the output of the form? So when a user selects the year 2007 for example, in the python file the variable x will change to '2007'?

Use request.form.get() to access data from a form with method POST.
Something like:
from flask import Flask, render_template, request
app = Flask(__name__)
def calculate(year):
mask = (df['start'] < x) & (df['end'] > year)
return df.loc[mask]
#app.route("/", mothods=['POST', 'GET'])
def home():
try:
input_year = request.form.get('yearInputName')
except:
input_year = '2005' # Default
# You may wish to add some validation for input_year here.
output = calculate(input_year)
return render_template("index.html", output=output)

If I understood you correctly, you basically want to change the actual python file base on the user input. Probably not. Because multiple users will have separate input.
Here's the things you might want(or might help you) regardless of what I understood.
To get the input from a post request -
Simply use request.form.get('name') to get the data.
Furthermore here.
You might want to use JavaScript to send post data.
Try this-
var element = document.getElementById('yearInputId')
var xhr = new XMLHttpRequest()
xhr.open('/url') \\the route you want this data to handle
xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded')
xhr.send("year=" + element.value)
You may want to store the year in a database or session variable
See using db with flask & using sessions
Storing in a session variable might do the work if you don't want to store the year but just as temporally.
If you want to serve '/' based on the post request
#check if request method is post
if request.method == 'POST':
year = request.form.get('year') #change year to your html post variable name
return render_template("index.html", output=year)
else if request.method == 'GET':
return render_template("index.html", output=output)
and yes, make sure to enable post request in the route.
#app.route('/', mothods=['POST', 'GET'])
Advices -
You probably want to make a separate route for the post data to process.
Use database or sessions depending on your goal. When serving future requests, just the parse the year. You may need to store a unique identifier. Like username or browser cookie along with the year.
Use JavaScript if you're updating something just locally.

Related

Is it possible to automate the passing of a prompt into the command line (terminal) and store the output in a new variable? [duplicate]

This question already has answers here:
Store large data or a service connection per Flask session
(1 answer)
How to pass a variable between Flask pages?
(2 answers)
Closed 10 days ago.
Using GPT3 and the user information from my web form, I want to create an AI generated application mail. So every time the user inputs his information into the front-end, I want my program(back-end) to pass the prompt: "Create me an application mail, which is sent by a person named David, contains the date 02/01/23 and goes to the company Netflix" into the command line(terminal) automatically and store the resulting output in a new variable.
David, 02/01/23, Netflix are just examples and would basically just be the variable values(=input of the user).
Take for example this code:
app.py:
from flask import Flask, render_template, request
app = Flask(__name__)
#app.route('/')
def index():
return render_template('index.html')
#app.route('/', methods=['POST'])
def getValue():
name = request.form.get('name')
date = request.form.get('date')
companyname = request.form.get('companyname')
ai.py:
import openai
def askGPT(text):
openai.api_key = ""
response = openai.Completion.create(
engine = "text-davinci-003",
prompt = text,
temperature = 0.6,
max_tokens = 150,
)
return print(response.choices[0].text)
def ai():
while True:
print('GPT: Ask me a question\n')
myQn = input()
askGPT(myQn)
print('\n')
ai()
So the getValue function stores the input of the user and the ai.py file is able to give me an ai generated answer when I input something into the command line(terminal).
How is it possible to store the values of the variables from app.py into a prompt, which is being automatically passed into the command line(terminal) of ai.py and how can we store the ai generated output into a new variable?
**** PS: You need an api key to run the ai.py file.

Flask - Take URL in Route? [duplicate]

This question already has answers here:
Python - Flask Default Route possible?
(4 answers)
Closed 7 months ago.
I have a question about Flask.
I want to use one endpoint to handle requests.
To do so, I need to take url in router like:
#app.route("/<url>", methods=['GET','POST'])
def home(url):
base_url = "https://www.virustotal.com/"
my_url = base_url + url
For example, I will sent request to my Flask app as " localhost:5000/api/v3/files/samehashvalue "
and it will combine it with virustotal url.
my_url will something like = virustotal.com/api/v3/files/samehashvalue
How can I pass /api/v3... to my router? Without using parameters like ?url=...
I'd suggest reading redirects from the Flask docs as well as url building.
With your specific example you can obtain the url from the route and pass it into your Flask function. It's then just a case of using redirect and an f string to redirect to that new url.
# http://localhost:5000/test redirects to
# https://www.virustotal.com/api/v3/files/test
from flask import Flask, redirect
app = Flask(__name__)
#app.route('/', defaults={'path': ''})
#app.route('/<path:path>')
def url_redirector(path):
return redirect(f'https://www.virustotal.com/api/v3/files/{path}')
if __name__ == '__main__':
app.run(debug=True)
I am not sure if this is correct, but I assume that you can specify the path in #app.route if it is a fixed path. For example:
#app.route("/api/v3/files", methods=['GET','POST'])
def home(url):
base_url = "https://www.virustotal.com/api/v3/files/"
Then the hash value only can be passed as a parameter.
Please let me know if I misunderstood your question and I will edit this answer.

How to set and access per request variable in Flask python

I want to read request argument and set it in some variable so that I should be able to access it anywhere down the line.I tried to use g and call context but it is giving me errors like
'Working outside of application context.'
Is it possible to achieve it using Flask?
Is there alternative solution to this problem?
Storing values globally not the best solution. And you can't store value in flask.g and expect to be available in the next request, well it's not. So I advise you to look into database or session to store value and access anywhere.
from flask import Flask, g
app = Flask(__name__)
#app.route("/")
def index():
if "count" not in g:
g.count = 0
g.count += 1
print(g.count)
return "This is index"
Use app_context. It keeps track of the application-level data during a request, CLI command, or other activity.
Read link for more infrmation
Example:
from app import app
with app.app_context():
print("Set or Access request variable")

How to handle multi users' requests separately in Flask?

I've written the following flask server:
from flask import Flask, render_template, request
import os
app = Flask(__name__)
# home
#app.route('/')
def home():
return 'HOME PAGE'
#app.route('/add')
def add():
global a
a += 1
return str(a)
if __name__ == '__main__':
a = 0
HOST = '10.10.10.10'
PORT = 5555
app.run(HOST, PORT)
Considering there are two users (from different IP addresses) of my server: A and B. When user A requests by url 10.10.10.10:5555/add, he gets the result 1. After that, if user B requests by url 10.10.10.10:5555/add he will get 2. Because two users share the same variable a
However, I want my server to handle A and B separately which means a user A and B have a variable a in their own way. The requests of A shouldn't affect the result that B will get. For example, When user A requests, he gets 1. After that user B requests and he should get 1 as well.
How should I modify my code to achieve this?
Based on your question, I think you're confused about the definition of "global".
In Flask, you have a Flask server with multiple threads and potentially multiple processes handling requests. you had a global variable a, and you wanted to keep adding to it in every request and want a variable to be independent.This is totally possible in theory and practice. It's also a really bad idea. This case actually create Deadlocks
The problem is that you can't easily control which threads and processes "win"
You should keep the webserver itself as stateless as possible. Each request should be totally independent and not share any state in the server. Instead, use a database or caching layer which will handle the state for you. This seems more complicated but is actually simpler in practice. Check out SQLite for example ; it's pretty simple.
Thanks to #n00dl3 's suggestion, I've managed to achieve the goal of my example. Here is the code:
from flask import Flask, render_template, request, session
import os
from datetime import timedelta
app = Flask(__name__)
app.config['SECRET_KEY'] = os.urandom(24)
app.config['PERMANENT_SESSION_LIFETIME'] = timedelta(days = 7)
# login
#app.route('/<username>', methods=['GET', 'POST'])
def home(username):
if username in session:
print(session.keys())
return 'hello {}'.format(username)
else:
session[username] = username
# generate this user's variable
a[username] = 0
print(session.keys())
return 'login as {}'.format(username)
# logout
#app.route('/logout/<username>', methods=['GET', 'POST'])
def logout(username):
session.pop(username)
print(session.keys())
return '{} logout!'.format(username)
# call add function with specific username
#app.route('/add/<username>')
def add(username):
global a
a[username] += 1
return str(a[username])
if __name__ == '__main__':
a = {}
#HOST = environ.get('SERVER_HOST', 'localhost')
HOST = '10.10.50.23'
try:
PORT = int(os.environ.get('SERVER_PORT', '5555'))
except ValueError:
PORT = 5555
app.run(HOST, PORT, debug=True)
However, I'm not sure if my way is a decent solution. So still listen to any better answers.
use different WSGI server to deploy your project. see this link http://flask.pocoo.org/docs/1.0/deploying/

How to do a normal functions in python flask without a route and return the results of this process using a route?

The app is loading the "links_submit.html" which has a field where you can write a link, like for example (www.google.com) and you submit it, then the app is receiving this URL as HTTP Post and redirecting to another page "post_response.html" which contains a simple html for feedback with the word "Ok". Then I want to do a process with this link (crawl google and search for a specific thing) and after finish this process, automatically redirect from the "post_reponse.html" to another page to show the results I have extracted from google. Now I'm not sure how say to my app on flask: "Ok now lets use a normal function (not route) like for example:
def loadpage(link_sent_by_the_http post):
res = requests.get('www.google.com')
Imagine that after load the page I also extract some html tag on google and after finish this process I want to redirect the page "post_respose.html" with the "ok" to a new html page which contains the html tag extracted from google.
Please note I know how to load the page google and extract what I want, but I don't know how to insert this function/process in the middle of Flask and then redirect from a normal html with "ok" for a new route with the results I have extracted.
import requests
from flask import Flask, render_template, request, url_for
app = Flask(__name__)
#app.route('/test')
def form():
return render_template('links_submit.html')
#app.route('/links/', methods=['POST'])
def links():
links=request.form['links']
return render_template('post_response.html')
Intern Process (Load the received link > Extract what I want)
and then redirect the "post_response.html" to another "html" which will
contain the results that I have extracted)
if __name__ == '__main__':
app.run(debug=True)
Two ways to do it-
Create a python file say webfunctions.py and put your function in this file.
e.g. -
def inc(x):
return int(x) + 1
Now in your flask app file, you can import the whole file or just the function-
from webfunctions import inc
#app.route('/whatsnext/', methods=['POST'])
def waiting():
curVal=request.form['x']
nextVal = inc(curVal)
return render_template('post_response.html', nextVal=nextVal)
or else, you may declare your definitions at the top of your flask app file. Like below -
import requests
from flask import Flask, render_template, request, url_for
def inc(x):
return int(x) + 1
#app.route('/whatsnext/', methods=['POST'])
def waiting():
curVal=request.form['x']
nextVal = inc(curVal)
return render_template('post_response.html', nextVal=nextVal)

Categories