简体   繁体   中英

How to set a crontab with html request using flask-crontab?

I'd like to run a html page where I use a button to set a specific time (see below) which later runs a cronjob via the module flask-crontab . How can I use minute , hour , day , month outside def get_time() without setting the variables global?
Whats a solid way to use flask-crontab here?

APP = Flask(__name__)
Bootstrap(APP)
crontab = Crontab(APP)

...

@APP.route('/randompage.html' methods = ['POST', 'GET])
def get_time():
    time_req = request.args.get("html_time")
    format_time = datetime.strptime(time_req, "%Y-%m-%dT%H:%M")

    minute = format_time.minute
    hour = format_time.hour
    day = format_time.day
    month = fomrat_time.month

    return render_template('randompage.html', time_req=time_req)


@crontab.job()
def exe_control():
    do something here

Button on html-page:

<form action="/randompage.html" method="GET">
<input type="datetime-local" name="html_time"/>
<input type="submit"/></form>

To use values minute, hour, day, month in other functions you have to use global variables or keep in global list / dictionary or save in file /database` and read in other functions.

But if you want these values to use as values in @crontab.job(minute=..., hour=...) then it is useless. You should run it directly in get_time as normal function

crontab.job(minute=minute, ...)(exe_control)


APP = Flask(__name__)
Bootstrap(APP)
crontab = Crontab(APP)

# ...

@APP.route('/randompage.html' methods = ['POST', 'GET'])
def get_time():
    time_req = request.args.get("html_time")
    format_time = datetime.strptime(time_req, "%Y-%m-%dT%H:%M")

    minute = format_time.minute
    hour = format_time.hour
    day = format_time.day
    month = fomrat_time.month

    crontab.job(minute=minute, hour=hour, day=day, month=month)(exe_control)

    return render_template('randompage.html', time_req=time_req)

# - without decorator -
def exe_control():
    do something here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM