Forums

Github Actions + PythonAnywhere API

Hey, I'm trying to use Github Actions to deploy my code on PythonAnywhere. The script is a simple Python script (main.py), already pre-uploaded to PythonAnywhere in advance, that should be run by Github Actions. The script "main.py" will pull new code changes in the repo from Github Actions, and then run the "app.py".

For some reason, this code just doesn't run the file:

  - name: Deploy to PythonAnywhere
    env:
      PYTHON_ANYWHERE_API_TOKEN: ${{ secrets.PYTHON_ANYWHERE_API_TOKEN }}
    run: |
      response=$(curl -s -o /dev/null -w "%{http_code}" -X POST \
      "https://www.pythonanywhere.com/api/v0/user/<username>/consoles/" \
      -H "Authorization: Token $PYTHON_ANYWHERE_API_TOKEN" \
      --data "executable=python3.10" \
      --data "command=python3.10 /home/<username>/main.py > /home/<username>/main_log_2.txt 2>&1")

I'm failing to troubleshoot this issue. Please assume that <username>=my_username

Thanks for guidance

Ok - some update. Apparently, I got an error number that the bash console must be already run or open in a tab, in order to run something in it. Now my problem is, that the yml file cannot execute the command. The console doesn't take "\n" as an execute, but just part of the string.

      response=$(curl -s -o /dev/null -w "%{http_code}" -X POST \
      "https://www.pythonanywhere.com/api/v0/user/username/consoles/${CONSOLE_ID}/send_input/" \
      -H "Authorization: Token $PYTHON_ANYWHERE_API_TOKEN" \
      --data "input=python3.10 /home/username/main.py\n")

curl data using url encode format, so to send line break replace \n with %0A and its works

What error are you seeing?

I tried the %0A and it didn't work either.

Here's my recent part of the workflow yml file:

       input="python3.10 /home/username/deploy.py"

       response=$(curl -s -X POST \
"https://www.pythonanywhere.com/api/v0/user/username/consoles/console_id/send_input/" \
         -H "Authorization: Token $PYTHON_ANYWHERE_API_TOKEN" \
         --data-urlencode "input=$input")

The response I get from the API is: Response: {"status":"OK"}

I can watch the console webpage where the "python3.10 /home/username/main.py\n" is added, but not executed.

You need to send the input in jSON. So, using requests, it would be like this:

response = requests.post(
    console_url + 'send_input/',
    headers={'Authorization': 'Token {}'.format(token)},
    json={'input': code + "\n"},
)

Thank you! It works. I also used it with curl, to anyone interested:

     # Send the command to run script.py in the Python interpreter via JSON format
      input='{"input": "python3.10 /home/username/script.py\n"}'

      response=$(curl -s -X POST \
        "https://www.pythonanywhere.com/api/v0/user/username/consoles/console_ID/send_input/" \
        -H "Authorization: Token $PYTHON_ANYWHERE_API_TOKEN" \
        -H "Content-Type: application/json" \
        --data "$input")

Glad to hear you worked it out!

Hey I have an action on GitHub for Django Application https://github.com/umuttopalak/pythonanywhere-deploy-action I also working on for deploying Flask applications

That's really useful, thank you for sharing it!