Skip to content
This repository was archived by the owner on Feb 4, 2021. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 8 additions & 7 deletions src/jupyter_notebook_gist/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,23 +72,23 @@ def get(self):
filename, filename_no_ext = get_notebook_filename(nb_path)

# Extract file contents given the path to the notebook
notebook_output, python_output = get_notebook_contents(nb_path)
notebook_output, script_output, ext = get_notebook_contents(nb_path)

# Prepare and our github request to create the new gist
filename_with_py = filename_no_ext + ".py"
filename_with_ext = filename_no_ext + ext
gist_contents = {
"description": filename_no_ext,
"public": False,
"files": {
filename: {"filename": filename, "content": notebook_output},
filename_with_py: {"filename": filename_with_py,
"content": python_output}
filename_with_ext: {"filename": filename_with_ext,
"content": script_output}
}
}

# Get the authenticated user's matching gist (if available)
match_id = find_existing_gist_by_name(filename,
filename_with_py,
filename_with_ext,
access_token)

# If no gist with this name exists yet, create a new gist
Expand Down Expand Up @@ -257,11 +257,12 @@ def get_notebook_contents(nb_path):
# Extract file contents given the path to the notebook
try:
notebook_output, _ = export_by_name("notebook", nb_path)
python_output, _ = export_by_name("python", nb_path)
script_output, script_resources = export_by_name("script", nb_path)

except OSError: # python 2 does not support FileNotFoundError
raise_error("Couldn't export notebook contents")

return (notebook_output, python_output)
return (notebook_output, script_output, script_resources['output_extension'])


def find_existing_gist_by_name(nb_filename, py_filename, access_token):
Expand Down
14 changes: 11 additions & 3 deletions tests/test_handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
raise_error, raise_github_error,
verify_gist_response)
from nbformat import write
from nbformat.v4 import new_markdown_cell, new_notebook
from nbformat.v4 import new_code_cell, new_markdown_cell, new_notebook


class TestError(unittest.TestCase):
Expand Down Expand Up @@ -326,17 +326,25 @@ def test_get_notebook_contents_notebook(self):
nbdir = os.getcwd()

nb = new_notebook(
metadata={
'language_info': {
'name': 'python',
'file_extension': '.py'
}
},
cells=[
new_markdown_cell(u'Testing')
new_markdown_cell(u'Testing'),
new_code_cell(u'x = 123')
])

with io.open(os.path.join(nbdir, fname), 'w',
encoding='utf-8') as f:
write(nb, f, version=4)

nb_content, python_content = get_notebook_contents(fname)
nb_content, python_content, ext = get_notebook_contents(fname)
self.assertTrue(len(nb_content) > 0)
self.assertTrue(len(python_content) > 0)
assert ext == ".py"

# Delete the temporary file
os.remove(nbdir + "/" + fname)
Expand Down