I'm trying to write a Python application that will execute certain code repeatedly on a timer when that timer is enabled. I'd like to be able to enable/disable the timer from a web page. I've been attempting to do this with Flask and the sched event scheduler, to no avail. I'm not sure if either Flask or sched are the right tools for the job, and I appreciate any guidance. Thanks!
My Code So Far:
from flask import Flask, render_template, request
import sched, time
app = Flask(__name__)
cameraScheduler = sched.scheduler(time.time, time.sleep)
def triggerCamera(sc):
print('Picture Taken')
cameraScheduler.enter(5,1,triggerCamera,(sc,))
#app.route('/', methods = ['POST','GET'])
def settings():
if request.method == 'POST':
if(request.form.getlist('timer') == ['true']):
cameraScheduler.enter(5,1,triggerCamera,(cameraScheduler,))
cameraScheduler.run
else:
cameraScheduler.cancel()
return render_template("settings.html")
if __name__ == '__main__':
app.run()
Try actually calling the scheduler cameraScheduler.run() (you are missing the parentheses there).
Related
I've just joined this platform, i am amateur at python, i tried to do something at python, i want to design a basic website with python flask module, i want to get data from other py files but when i run the main program data comes just once, i want to get data frequently like each 1 seconds etc. i want to get "c" value (at from plc.py) each second. like auto update from other py file.
my main program is;
from multiprocessing import Process,Queue,Pipe
from flask import Flask, jsonify, render_template, request
from flask_apscheduler import APScheduler
import webbrowser
import time
from plc import c
app = Flask(__name__)
scheduler = APScheduler()
#app.route('/_stuff', methods = ['GET'])
def stuff():
return jsonify(result=c[3]) # c[3] yerine time.asctime() yazınca değişken veri geliyor
def scheduledTask():
print(c)
return (c)
#app.route('/')
def index():
return render_template('dy1.html')
if __name__ == '__main__':
scheduler.add_job(id='Scheduled task', func=scheduledTask, trigger='interval', seconds=5)
scheduler.start()
app.run(debug=True, use_reloader=True)
I have a flask app script that has multiple routes...
#app.py
def create_app(Tractor_id=0):
#app.route("/")
def index():
return render_template('index.html')
#app.route("/id")
def start():
return Tractor_id
#app.route("/stop")
def stop():
I'm trying to implement it multiple times with different parameters each time with the help of DispatcherMiddleware but I'm having trouble with it.
Here's the actual implementation:
# multiapp.py
from werkzeug.middleware.dispatcher import DispatcherMiddleware
from werkzeug.serving import run_simple
import start
T1 = start.create_app(Tractor_id='101')
T2 = start.create_app(Tractor_id='102')
# merge
application = DispatcherMiddleware(
None, {
'/{}'.format('T101'): T1,
'/{}'.format('T102'): T2
}
)
if __name__ == '__main__':
run_simple(
hostname='localhost',
port=5000,
application=application,
use_reloader=True,
use_debugger=True,
use_evalex=True)
In the index.html there are buttons that should redirect the user to the /id and /stop routes, but the do not work.
The general question would be, how to run multiple Flask applications which each have multiple routes within them?
I dont know DispatcherMiddleware.
But if you are trying to bind few flask servers, just pick for every process another port and it will work.
When you request them of course pay attention to port.
If you are struggling with actually making a multi route server, heres a good example.
I have read through multiple SO answers regarding asyncio to figure out how to accomplish this. However, the answers that i found didn't work for the problem i'm having.
I have a flask app that I want to serve while running a background function. I have the following code:
import asyncio
import time
from flask import Flask
def background_task():
while True:
print('hej')
time.sleep(1)
app = Flask(__name__)
#app.route('/test')
def hello_world():
return 'Hello, world'
asyncio.run(background_task())
app.run()
print('running')
I want background_task to keep running while serving the app. But currently asyncio.run blocks the main thread. How do i make it so that the background_task is run as a coroutine?
Try the threading module instead, I've found it useful, here's an example:
import threading
import asyncio
import time
from flask import Flask
def background_task():
while True:
print('hej')
time.sleep(1)
app = Flask(__name__)
#app.route('/test')
def hello_world():
return 'Hello, world'
#We start a thread instead of using asyncio...
t=threading.Thread(target=background_task)
t.start()
app.run()
print('running')
And here's a tutorial: https://realpython.com/intro-to-python-threading/
I hope this helps you.
I am new to Python Flask and need some information/basic knowledge on how to use Signals with Flask.
My understanding so far:
I can create and send own signals. I can use this signal, to make a function call.
My Flask Application looks something like this:
#app.route("/")
def start():
return render_template('index.html')
#app.route("/search")
def search():
rThread = threading.Thread(target=getTags)
rThread.start()
return redirect(url_for('search'))
The getTags in rThread does somemthing outside the application.
But basically it looks like this:
def getTags():
#do something#
#now its finished#
I want to send a signal when getTags is done and the Flask application should get this signal and redirect to index.html.
I am stuck on creating a signal, but I have no clue how to send the signal and how to tell the flask app to redirect to index.html if the signal is sent.
Thanks for any help and advice.
EDIT: For clarification, there are some more pages a user can go to.
#app.route("/details")
def det():
#do stuff
return render_template('details.html')
#app.route("/admin")
def adm():
if request.method == "GET":
#do Stuff
return redirect(url_for('search'))
else
#do Stuff
return render_template("admin.html")
If you just want to wait for a function to do its job you can simply call the function and then respond after it. You used threading.thread so you can use threading.Event and wait for the event to set by the function:
from flask import Flask, render_template, url_for, redirect
from threading import Thread, Event
from time import sleep
app = Flask(__name__)
event = Event()
#app.route("/")
def start():
render_template('index.html')
#app.route("/search")
def search():
rThread = Thread(target=getTags)
rThread.start()
event.wait()
event.clear()
return redirect(url_for('start'))
def getTags():
print("doing some processing things")
sleep(5)
print("done")
event.set()
if __name__ == "__main__":
event.clear()
app.run(host="localhost", port="8080", debug=True)
It not possible for a web server to trigger an event in client or web browser.
Write a java script function that run for certain interval of time and get data from server and then if the result is required then you redirect to another page.
for reasons I want to trigger the reboot of an raspberry pi using a REST api.
My REST api is coded in python flask like this:
from flask import Flask
from flask import jsonify
import subprocess
app = Flask(__name__)
#app.route('/api/reboot')
def reboot():
subprocess.call("/sbin/reboot")
return jsonify(triggered='reboot')
if __name__ == '__main__':
app.run(debug=True,host="0.0.0.0")
the code is working perfectly fine. But due to its a reboot the return will not be send (because the system is rebooting obviously).
Is there a way to trigger the reboot some kind of async with a delay of a couple milliseconds, that allows to return some value (in my case just an custom 'ack') prior the actual reboot?
Try threading.Timer:
For example:
from flask import Flask
from flask import jsonify
import subprocess
import threading
app = Flask(__name__)
def _reboot():
subprocess.call("/sbin/reboot")
#app.route('/api/reboot')
def reboot():
t = threading.Timer(1, _reboot)
t.start()
return jsonify(triggered='reboot')
if __name__ == '__main__':
app.run(debug=True,host="0.0.0.0")