Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

syncing Github with Pythonanywhere

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.

like image 977
manisha katariya Avatar asked Jan 01 '18 04:01

manisha katariya


3 Answers

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:

  • I develop in Visual Studio on my local computer and push my code to my Github repository
  • Github automatically emits a post-receive webhook with a json file that I listen to on my pythonanywhere server
  • In my python script I simply execute a pull command as soon as the webhook URL gets triggered. After that all my files on pythonanyhwere are up-to-date

Tips:

  • If you have not yet initiated git on your pythonanywhere project, just open a bash console, navigate to your root folder, e.g. "home/username" and enter git init, then git remote add origin https://github.com/yourusername/yourreponame.git
  • You can create the post-receive webhook in your github repository's settings page
  • I use the GitPython package to execute the pull request
  • Below is the python code I use in my flask web server to wait for the webhook to execute. It basically executes a predefined bash command that gets created automatically in your pythonanywhere file structure and that is located under .git/hooks/. This bash file will execute a simple git pull origin master

The content of my flask_app.py file:

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.

like image 112
Wanni Avatar answered Sep 18 '22 10:09

Wanni


You could consider:

  • developing locally (See GitHub help to create an account, a new repo, and clone it locally)
  • pushing and updating your repo/app on pythonanywhere, as described in "Git push deployments on PythonAnywhere"

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".

like image 36
VonC Avatar answered Sep 18 '22 10:09

VonC


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)
like image 23
suhailvs Avatar answered Sep 22 '22 10:09

suhailvs