2

I have a Flask app that takes in user input and then passes that input along to other functions in the program. My problem is that the other functions will not run unless I close out of the Flask and shut down the server. How would I keep the server up and call the functions? Essentially once the user hits 'submit' I want all the functions to run.

Starting the flask app

app.run(host = '127.0.0.1', threaded = True)

Passing variables into HTML form input

@app.route('/server_app', methods=["POST"])
def server_app():
    global set_freq, set_center_freq
    set_freq = request.values.get("set_freq")  # <--- do whatever you want with that value
    set_center_freq = request.form.get("set_center_freq")
    return render_template("main_page.html")

Test function is then called, all it does for now is print one of the variables, but it won't run until I use control c and close out of the server.


    def test_fun(set_freq):
        print(set_freq)

    #main
    app.run(host = '127.0.0.1', threaded=True)
    test_fun(set_freq)

it only runs 'test_fun' once I manually stop the flask server from running.

1 Answers1

-1

nice to meet you. If you want to run a function you should just be able to define it and run it using def and then run it when you get the data. If there is something that isn't working can you update your question with that specifically. For example what function are you trying to run? So here's some code to go along with it

#Dependencies
from flask import Flask,render_template,session,redirect,url_for,jsonify,send_from_directory


# App stuff
app = Flask(__name__)
def test_fun(set_freq):
    print(set_freq)
#Routers
@app.route("/",methods=["GET"])
def home():
    return render_template("index.html")

@app.route('/server_app', methods=["POST"])
def server_app():
    global set_freq, set_center_freq
    set_freq = request.values.get("set_freq")  # <--- do whatever you want with that value
    set_center_freq = request.form.get("set_center_freq")
    test_fun(set_freq)
    return render_template("main_page.html")



#Run
if __name__ == "__main__":
    app.run(host="0.0.0.0",port=5000)
smal
  • 177
  • 10
  • I updated the question for you. The test function only runs once the Flask web server is manually stopped by me. I need to have that function run once I have the variables populated while the server continues running. –  May 03 '21 at 15:24
  • Ok, this makes more sense. Here's a previous question that should help. If it does not explain things enough just ask again here and I will be happy to help you.https://stackoverflow.com/questions/27465533/run-code-after-flask-application-has-started – smal May 03 '21 at 15:25
  • To be honest they dont look like the cleanest of bits of code anyway. What I would do, and this may not be the best solution, is if its about right when the first request is called. have a global variable that says if it is the first request and do it then. This will only work if the function does not take much time btw. Good luck and if you would like some help here are some flask templates I wrote a while back: https://github.com/shoryamalani/templates/tree/master/flask – smal May 03 '21 at 15:28
  • My problem is that I need to wait until input is entered, essentially I have to launch the server, take input, then run that other function, while the server is still running as well. –  May 03 '21 at 15:29
  • Ahhh gotcha. So basically what you want to do is call the function from your def home(): this is just an example but you get what I mean If you don't I can write the code for you – smal May 03 '21 at 15:31