I want to sync pythonanywhere project with github account. Like If I make changes in my project at github,it automatically gets updated at pythonanywhere. Forgive me I am new to github.
I just solved this issue for my own Pythonanywhere project. I did not want to bother with SSH keys, so I used Github webhooks and a Python script that runs on my pythonanywhere account. The Python script listens to the webhook that Github emits when the source code has been updated and executes a script on pythonanywhere to pull in the new files.
Here is the scenario:
Tips:
git init
, then git remote add origin https://github.com/yourusername/yourreponame.git
.git/hooks/
. This bash file will execute a simple git pull origin master
from flask import Flask, request
import git
app = Flask(__name__)
@app.route('/webhook', methods=['POST'])
def webhook():
if request.method == 'POST':
repo = git.Repo('./myproject')
origin = repo.remotes.origin
repo.create_head('master',
origin.refs.master).set_tracking_branch(origin.refs.master).checkout()
origin.pull()
return '', 200
else:
return '', 400
#
# Below here follows you python back-end code
#
Let me know if you need more info.
You could consider:
If you want to develop only on pythonanywhere, you would need to generate an SSH key, and add the public one to your GitHub account, as suggested in "How to get your code in and out of PythonAnywhere".
For Django:
first you need to install gitpython: pip install gitpython
update views.py
:
from django.http import HttpResponse
from git import Repo #
from django.views.decorators.csrf import csrf_exempt
@csrf_exempt
def webhook(request):
if request.method == 'POST':
repo = Repo('./django-schools')
git = repo.git
git.checkout('master')
git.pull()
return HttpResponse('pulled_success')
return HttpResponse('get_request', status=400)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With