I am relatively new to the complete Flask package and I am facing a complex situation. I want the below code only to run once, but it asks me to PRESS Ctrl+C to quit. It's fairly understandable. But as I wish to use the value from a REST API for some other purpose, I want the script to automatically exit the app.run command.
The script works perfectly fine once I press the Ctrl+C.
I have also given the app.before_first_request, but that results in a str object not callable error.
Could someone please come to my rescue.
Thanks :)
Below is my code:
from flask import Flask
from flask import jsonify
import sys
app=Flask(__name__)
@app.route('/')
def hello_world():
return jsonify(result='Hello World')
@app.route('/leds/<pin>/<lednr>/<status>')
def lednr_regal_leds(pin,lednr,status):
global a
a=(pin,lednr,status)
print(a)
return jsonify(pin,lednr,status)
app.run(host='0.0.0.0',port='5000')
print(a)
# Use the value a further.
Flask is a web server. It's meant to be long running.
You cannot just start the script and then it ends, meanwhile the API is still available
If you want to stop the server within a route, then find out what method off the app variable can do that