Forums

Scheduling a Task using PythonAnywhere API

I have an app that allows users to schedule a task. What would be the best way to implement this? I have tried using the python anywhere API, so when the user schedules a task, it gets created on python anywhere. Is this the best way to do this? The API is working okay, but the task doesn't seem to be showing on my dashboard, but I get a 200 status code response. Any ideas?

Sure. That should work. Check the content of the response to see what might be happening to your tasks.

On the documentation it says that I need to pass a command and enabled as a parameter in the request. What format do I have to do these in? Or is there an example for scheduled tasks I can follow?

The code should look something like this:

    params = {
        "interval": "hourly",
        "minute": 15,
        "hour": 3,
        "command": "some command",
        "enabled": True
    }
    response = requests.post(
        "https://eu.pythonanywhere.com/api/v0/user/your-username/schedule/
        json=params,
        headers={"Authorization": "Token your-token"}, 
    )
    assert response.status_code == 201