From 242b48d3d6f1ea6bda4175cae227bb13877e3f8a Mon Sep 17 00:00:00 2001 From: KBolashev Date: Thu, 7 Sep 2023 12:15:15 +0300 Subject: [PATCH 1/2] Fix for path handling in save_notebook function --- dagshub/notebook.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/dagshub/notebook.py b/dagshub/notebook.py index 0a8619ef..79f7999c 100644 --- a/dagshub/notebook.py +++ b/dagshub/notebook.py @@ -2,7 +2,7 @@ import json import logging import tempfile -from pathlib import PosixPath +from pathlib import Path, PurePosixPath from socket import gethostname, gethostbyname import httpx @@ -59,10 +59,11 @@ def save_notebook(repo, path="", branch=None, commit_message=None, versioning='g return # Handle file path - file_path = PosixPath(path) + file_path = Path(path) if file_path.name != "." and "." not in file_path.name: file_path /= _default_notebook_name() file_path = "/" / file_path + remote_path = PurePosixPath("/") / file_path.as_posix() # Handle commit message if commit_message is None: @@ -79,14 +80,17 @@ def save_notebook(repo, path="", branch=None, commit_message=None, versioning='g out_path = f"{tmp}/{file_path.name}" if _inside_colab(): from google.colab import _message # If inside colab, this import is guaranteed + notebook_ipynb = _message.blocking_request("get_ipynb") + if notebook_ipynb is None or "ipynb" not in notebook_ipynb: + raise RuntimeError("Couldn't get notebook data from colab.") with open(out_path, 'w') as file: - file.write(json.dumps(_message.blocking_request('get_ipynb')["ipynb"], indent=4)) + file.write(json.dumps(notebook_ipynb["ipynb"], indent=4)) else: get_ipython().run_line_magic('notebook', out_path) repo = Repo(owner, repo, branch=branch) repo.upload(out_path, - remote_path=file_path.as_posix(), + remote_path=remote_path.as_posix(), commit_message=commit_message, versioning=versioning, force=True) From abfd242a5a0d4fbe03414002df55f1863ff7784c Mon Sep 17 00:00:00 2001 From: KBolashev Date: Thu, 7 Sep 2023 17:48:08 +0300 Subject: [PATCH 2/2] Add a warning about only saving the history in jupyter --- dagshub/notebook.py | 1 + 1 file changed, 1 insertion(+) diff --git a/dagshub/notebook.py b/dagshub/notebook.py index 79f7999c..c6dcb5ea 100644 --- a/dagshub/notebook.py +++ b/dagshub/notebook.py @@ -86,6 +86,7 @@ def save_notebook(repo, path="", branch=None, commit_message=None, versioning='g with open(out_path, 'w') as file: file.write(json.dumps(notebook_ipynb["ipynb"], indent=4)) else: + log_message("Saving only the execution history for the notebook in Jupyter environments", logger) get_ipython().run_line_magic('notebook', out_path) repo = Repo(owner, repo, branch=branch)