I am writing an application which will allow users to set up python scripts to run at set intervals. I am using APSCheduler combined with SQLAlchemy as a data source and Flask for my web UI.
I have a screen with the following markup for setting up jobs:
<form method="POST">
<input type="text" name="JobDesc">
<input type="text" name="function">
<input type="text" name="args">
<input type="text" name="kwargs">
<select name="trigger">
<option value="interval" selected>Interval</option> <!--Adding other job types later-->
</select>
<input type="number" name="minutes">
<select name="coalesce">
<option value=1>True</option>
<option value=0 selected>False</option>
</select>
<input type="submit">
</form>
This forms "submit" button triggers the below script, to add the record to the database
@app.route('/addjob', methods=['GET', 'POST'])
def JobConfig():
if flask.request.method == 'POST':
if flask.request.form['args'] == '': # '' == None returns False. :(
args = None
else:
args = flask.request.form['args']
if flask.request.form['kwargs'] == '':
kwargs = None
else:
print(flask.request.form['kwargs'])
kwargs = flask.request.form['kwargs']
scheduler.add_job(
trigger=str(flask.request.form['trigger']),
func=str(flask.request.form['function']),
minutes=int(flask.request.form['minutes']),
coalesce=bool(flask.request.form['coalesce']),
args=args,
kwargs=kwargs,
name=str(flask.request.form['JobDesc']),
)
return 'Success', 201
elif flask.request.method == 'GET':
return flask.render_template('JobEntry.html')
else:
return flask.abort(400)
APScheduler has a number of key fields:
- Function: Details of the function to be scheduled (for example: JOBS.TestJob:Test runs the Test function in the the Testjob.py file in the JOBS folder).
- args: List of functions to supply to the function.
- kwargs: Dictionary of functions to supply to the function.
Unfortunately, while the entry screen works fine when args and kwargs are blank, if you try to entry any arguments I get an error. For example, if I the Test function is expecting an argument "Abbrev" and I enter {"Abbrev":"Test1"} into the kwargs field:
File "C:\Python34\lib\site-packages\apscheduler\schedulers\base.py", line 334, in add_job
'kwargs': dict(kwargs) if kwargs is not None else {},
ValueError: dictionary update sequence element #0 has length 1; 2 is required
Would anyone be able to point me in the right direction? I'm not sure if this is an error in my python of if I need to use something other than vanilla HTTP POST requests (JSON?) to send the data.
This would indeed work better with JSON input. As the error implies, you need to give a dict as the "kwargs" argument and a tuple or list as the "args" argument. Alternatively you can parse the kwargs into a dict yourself, however you wish.