diff --git a/README.md b/README.md index 4ad0747..b1095a3 100644 --- a/README.md +++ b/README.md @@ -14,20 +14,23 @@ You can currently install this directly from git: ``` pip install git+https://github.com/sat28/githubcommit.git jupyter serverextension enable --py githubcommit -jupyter nbextension install --py githubcommit +jupyter nbextension install --py githubcommit --user ``` To enable this extension for all notebooks: ``` -jupyter nbextension enable --py githubcommit +jupyter nbextension enable githubcommit --user --py ``` ## Steps +I have made this short video in which I install the extension from scratch (click on the image)- + +[](https://www.youtube.com/watch?v=1XPSK1mxfgQ) * Install package using above commands -* Create Git repo where notebooks will be pushed if not already exists and clone it in home directory -* Clone this repo as well in home directory +* Create Git repo where notebooks will be pushed if not already exists and clone it in your `GIT_PARENT_DIR` +* Clone this repo as well in your `GIT_PARENT_DIR` directory * Replace the values in env.sh present in this repo itself * Run the command - source ~/githubcommit/env.sh * Configure ssh key (present in ~/.ssh/id_rsa.pub or specified location) in github account diff --git a/env.sh b/env.sh index 497dea7..108f3d8 100644 --- a/env.sh +++ b/env.sh @@ -1,5 +1,5 @@ ####################### SSH KEY FOR GIT ################################### -ssh-keygen -t rsa && ssh-keygen -y -f ~/.ssh/id_rsa > ~/.ssh/id_rsa.pub && eval `ssh-agent -s` && ssh-add -k +eval `ssh-agent -s` && ssh-add -k ####################### To be added to git account settings ################ @@ -23,7 +23,7 @@ export GIT_REMOTE_UPSTREAM=$GIT_USER_UPSTREAM/$GIT_REPO_NAME ####################### Git Repo where notebooks will be pushed ############ -cd ~ && git clone $GIT_REMOTE_URL_HTTPS +cd $GIT_PARENT_DIR && git clone $GIT_REMOTE_URL_HTTPS @@ -34,4 +34,4 @@ fi echo 'c.NotebookApp.disable_check_xsrf = True' >> ~/.jupyter/jupyter_notebook_config.py -cp ~/githubcommit/config ~/.ssh/config \ No newline at end of file +cp $GIT_PARENT_DIR/githubcommit/config ~/.ssh/config diff --git a/githubcommit/handlers.py b/githubcommit/handlers.py index b84d0d3..13228df 100644 --- a/githubcommit/handlers.py +++ b/githubcommit/handlers.py @@ -3,6 +3,7 @@ import os, json, git, urllib, requests from git import Repo, GitCommandError from subprocess import check_output +import subprocess class GitCommitHandler(IPythonHandler): @@ -32,6 +33,8 @@ def put(self): data = json.loads(self.request.body.decode('utf-8')) filename = urllib.parse.unquote(data['filename']) msg = data['msg'] + commit_only_source = data['commit_only_source'] + # get current directory (to return later) cwd = os.getcwd() @@ -54,8 +57,15 @@ def put(self): # commit current notebook # client will sent pathname containing git directory; append to git directory's parent try: - print(repo.git.add(str(os.path.expanduser('~') + "/" + os.environ.get('GIT_REPO_NAME') + filename))) - print(repo.git.commit( a=True, m="{}\n\nUpdated {}".format(msg, filename) )) + if commit_only_source : + subprocess.run(['jupyter', 'nbconvert', '--to', 'script', str(os.environ.get('GIT_PARENT_DIR') + "/" + os.environ.get('GIT_REPO_NAME') + filename)]) + filename = str(os.environ.get('GIT_PARENT_DIR') + "/" + os.environ.get('GIT_REPO_NAME') + filename.replace('ipynb', 'py')) + else: + filename = str(os.environ.get('GIT_PARENT_DIR') + "/" + os.environ.get('GIT_REPO_NAME') + filename) + + print(repo.git.add(filename)) + print(repo.git.commit( a=False, m="{}\n\nUpdated {}".format(msg, filename) )) + except GitCommandError as e: print(e) self.error_and_return(cwd, "Could not commit changes to notebook: {}".format(git_dir_parent + filename)) diff --git a/githubcommit/static/main.js b/githubcommit/static/main.js index e5f25b4..23ce7d7 100644 --- a/githubcommit/static/main.js +++ b/githubcommit/static/main.js @@ -13,6 +13,9 @@ define(['base/js/namespace','base/js/dialog','jquery'],function(IPython, dialog, var input = $('') var div = $('
') + var checkbox = '' + + div.append(checkbox) div.append(p) .append(input) @@ -24,7 +27,8 @@ define(['base/js/namespace','base/js/dialog','jquery'],function(IPython, dialog, var filepath = window.location.pathname.match(re)[1]; var payload = { 'filename': filepath, - 'msg': input.val() + 'msg': input.val(), + 'commit_only_source': $("#commit_only_source").prop('checked') }; var settings = { url : '/git/commit', @@ -83,6 +87,7 @@ define(['base/js/namespace','base/js/dialog','jquery'],function(IPython, dialog, notebook:env.notebook, keyboard_manager: env.notebook.keyboard_manager, }) + } }