Forums

starting, stopping scheduled task from flask app/browser

Hi, I am making a flask app with react and python.. i am wanting to make always on scheduled tasks , but start them and stop them with a click event on my web page. is this possible? I am a novice with all of this but learning.

It is possible using our API

can i display the logs in the ui as well? using the api?

Yes, they are just text files, so you can display them in the browser -- in the tasks table there is "Actions" column, clicking on the first icon there will open the task's log file. You can use our api as well, there's a cli tool that can be helpful, see https://github.com/pythonanywhere/helper_scripts.

yes, i was talking about using the api to call the logs. i will check out the link you posted.

can you check out my post code really quick to see what I am doing wrong on this api post, i am just trying to test this to get it to work. I was able to successfully get all the tasks running from the api However, I am having trouble doing a post and starting a task..Can you check this out?

Thks you can see my code here..the json is an unused argument https://ibb.co/12zC1wR

What response do you get back with your code?

here is the screen shot

https://ibb.co/cJG1ZKX

something wrong with my parameters and the json is not being used..

Looks like keys of the dictionary you pass to json kwarg should be in quotes. Is it even running in that shape?

keys being the parameters? i tried putting these in quotes before and got the same error.. i will do a screen shot again. with them in quotes? https://ibb.co/1R4L2Nd

what do you mean is it running in that shape? if it was running, I wouldnt be commented and asking for help..

I am able to get info from the api but not able to post. so in a hence, it is running but I am having trouble posting to the api with the code shown in the link.

Thks

Interesting, I can't reproduce that behavior -- did you save and run after making changes in the file?

Yep. I did. What are you getting when you run the last code i sent?

Oh! I see what happend :) You put json kwarg inside the format call! It should be outside like headers.

I am getting the same error with the last 2 codes i sent.

Do it like that: requests.post(url, headers=headers, json=params), where:

  • url is "https://www.pythonanywhere.com/api/v0/user/username/always_on/"
  • headers is what you have now
  • params is what you have in json now.

yea i just fixed it, before i saw this message.. sorry, i think i sent the other messaage at the same time you sent mine!

just put the json kwarg rt in with the headers.. thks so much, simple fix

Glad you got that working!

thks so much ! you have been a lot of help "pafk"

Lastly, I am trying to get the logs of the task by api..

I have tried the get info api call > /api/v0/user/{username}/always_on/{id}/

but getting a 404... here is the code https://ibb.co/6wMgKtL

IS there another api call or am I doing it wrong? I could not find any other api calls in getting information.

ah, actually, I messed up and did not have the bot started! I got this get info api to call, which has the > logfile:true...

Do you know how I can grab and print this logfile from the api and which url to call?

here is my code here.. https://ibb.co/x5Xd7Vb

Thks!

Hmm, that's an interesting point! The "logfile" URL that the API endpoint returns is for the UI, not the API. We should look into tweaking that so that it's more useful.

You can change it into something more useful by removing the /user/khundev/files/ from the start (so that it starts with /var/log, and is a simple filesystem path. Then you can use our files api to download it.

What i am trying to do is display that logfile to the ui. Is there is a simple easy way to throw a small text window displaying the log files the exact same way it is done on pythonanwhere? By calling the api on my site? Sorry if maybe u already answered it. Kinda new to this

Take a look at examples how to use our file api https://blog.pythonanywhere.com/180/

Ok. Thks, ill check it out

Let us know if it helped!

man, i hate bothering u guys but at least its most likely simple stuff!

I am getting a response but it looks like it is for the ui? like u said?

I was wanting to get the log file presented the same as when u click it in the task, which shows my values.

Here is a screen shot of the code. https://ibb.co/3hPf4F7

You're using a wrong url -- files/path part is missing.

thks, that did it.. i got the log file. it isnt as pretty as you guys and its all in a big block of code.. but i got it.. thks

Glad to hear that you made it!