diff --git a/jupygit/GitHandlers.py b/jupygit/GitHandlers.py index 31c62b6..eb981ec 100644 --- a/jupygit/GitHandlers.py +++ b/jupygit/GitHandlers.py @@ -5,6 +5,21 @@ from notebook.base.handlers import IPythonHandler +class GitRestoreHandler(IPythonHandler): + + file_suffix = "-jupygit___.ipynb" + + def post(self): + data = parse_qs(self.request.body.decode('utf8')) + dirty_path = data["path"][0] + + clean_path = dirty_path[:-len(self.file_suffix)] + ".ipynb" + os.remove(clean_path) + + self.write({'status':200,'content':'Hola'}) + self.flush() + + class GitCleanHandler(IPythonHandler): file_suffix = "-jupygit___.ipynb" diff --git a/jupygit/__init__.py b/jupygit/__init__.py index c32738a..431d6c7 100644 --- a/jupygit/__init__.py +++ b/jupygit/__init__.py @@ -1,13 +1,15 @@ from notebook.utils import url_path_join -from .GitHandlers import GitCleanHandler +from .GitHandlers import GitCleanHandler, GitRestoreHandler def load_jupyter_server_extension(nb_server_app): web_app = nb_server_app.web_app host_pattern = '.*$' web_app.add_handlers(host_pattern, [ (url_path_join(web_app.settings['base_url'], r'/git/clean'), - GitCleanHandler) + GitCleanHandler), + (url_path_join(web_app.settings['base_url'], r'/git/restore'), + GitRestoreHandler) ]) def _jupyter_nbextension_paths(): diff --git a/jupygit/static/index.js b/jupygit/static/index.js index d6be406..57e5c1e 100644 --- a/jupygit/static/index.js +++ b/jupygit/static/index.js @@ -10,11 +10,13 @@ define([ Jupyter.original_name = ""; function make_request() { + var clean_url = utils.url_path_join(utils.get_body_data('baseUrl'), 'git/clean') + var restore_url = utils.url_path_join(utils.get_body_data('baseUrl'), 'git/restore') + var cookies = getCookies(document.cookie); + var _xsrf = cookies["_xsrf"]; + var notebook_path = Jupyter.notebook.notebook_path; + if(Jupyter.original_name === "") { - var clean_url = utils.url_path_join(utils.get_body_data('baseUrl'), 'git/clean') - var cookies = getCookies(document.cookie); - var _xsrf = cookies["_xsrf"]; - var notebook_path = Jupyter.notebook.notebook_path; Jupyter.original_name = Jupyter.notebook.notebook_name; var new_name = Jupyter.original_name.substring(0, Jupyter.original_name.length - 6) + file_suffix @@ -37,6 +39,25 @@ define([ } }); }); + } else { + data = { + 'name': Jupyter.original_name, + 'path': notebook_path + } + $.ajax({ + type: "POST", + headers: { + "X-XSRFToken": cookies["_xsrf"] + }, + url: restore_url, + data: data, + success: function(d) { + Jupyter.notebook.rename(Jupyter.original_name).then(function (){ + Jupyter.original_name = ""; + alert("You can now keep working"); + }); + } + }); } } diff --git a/make.bat b/make.bat new file mode 100644 index 0000000..52c868a --- /dev/null +++ b/make.bat @@ -0,0 +1,4 @@ +pip install -e . +jupyter serverextension enable --py jupygit --sys-prefix +jupyter nbextension install --py jupygit --sys-prefix +jupyter nbextension enable --py jupygit --sys-prefix \ No newline at end of file