I'm running a flask webserver with a raspberry pi 3B+. The html website has some text, and two buttons. One button to run a external python script (not the flask python script) and one button to stop the python script.
So far, pressing the "start" button makes my external python script run just how I want it to. That works perfectly and I have no complaints about that
I want my "stop" button to stop the external python script that I started running with the "start" button. The script has a never ending while loop in it.
I've tried everything I can think of:
- creating a 'running' boolean variable, so that the while loop only runs when that variable equals true, and then using the stop button to change that variable.
- Having a "exit()" function inside of my external script that I call when my "stop" button is pressed.
None of that works. It just causes my webserver to crash and my external script keeps running (lol)
I get "TypeError: The view function did not return a valid response."
My flask code:
from flask import Flask, render_template
import miniMSA_Version2 as MSA
app = Flask(__name__)
@app.route('/')
def index():
return render_template('index.html')
@app.route('/run')
def run_MSA():
return MSA.run()
@app.route('/stop')
def stop_msa():
return MSA.stop()
if __name__ == "__main__":
app.run(host="0.0.0.0", port=80, debug=True)
My html code:
<!doctype html>
<head>
<title> MSA Webpage </title>
<meta charset=utf-8>
</head>
<body>
<h1> heading </h1>
<h2> 2nd heading </h2>
<p> cool paragraph </p>
<button> <a href="/run"> Start </a> </button>
<button> <a href="/stop"> Stop </a> </button>
</body>
External Python script (Note: This is a dumbed down version of my external script, it's over 170 lines long and I have no way to copy and paste it as my Raspberry pi doesn't have internet, so yes I copied all of the code in my post by hand):
import some stuff
import some more stuff
def run():
#create output file name etc
def get_data():
# run, collect and append data to output file
while True:
#flash led code here
# call get_data() function