I need to make a flask server that allows the user to enter an input then collects and saves that input. In another program I import my flask program to retrieve the variable and print it, but the program does not fetch the user’s input.
I have my flask code (app.py):
from flask import Flask, request, render_template
app = Flask(__name__)
user_input = None
@app.route("/", methods=["GET", "POST"])
def index():
global user_input
if request.method == "POST":
# Store the input in the global variable
user_input = request.form.get("user_input")
return f"Stored input: {user_input}"
return render_template("index.html")
def get_user_input():
return user_input
if __name__ == "__main__":
app.run(host="0.0.0.0", port=5000, debug=True)
I have my program to print input code:
import app
if __name__ == "__main__":
user_input = app.get_user_input()
if user_input:
print(f"Accessed user input from app.py: {user_input}")
else:
print("No input has been stored yet.")
I have my index.html code:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>User Input</title>
</head>
<body>
<form method="POST">
<label for="user_input">Enter something:</label>
<input type="text" id="user_input" name="user_input">
<button type="submit">Submit</button>
</form>
</body>
</html>
You need to sign in to view this answers
Leave feedback about this