-
Notifications
You must be signed in to change notification settings - Fork 18
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add basic functionality (WIP) #1
Merged
Changes from 4 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
a12618a
Add basic functionality
SaladRaider e8205cd
Remove old notebook.zip file
SaladRaider b50cf80
Refactor emits
SaladRaider 30b40bc
Add useful info and demo to README
SaladRaider 23dc5b6
Change html element id to better name
SaladRaider ad86fd9
Add tests for checking json output of eventstream API
SaladRaider 10cac10
Rename travis file to correct file name
SaladRaider ba76fc8
Sort contents of eventstream json to correctly compare equivalent out…
SaladRaider 3fd84d2
Add test that checks the integrity of the zipped files
SaladRaider 2f747a2
Stream zip file out to user without creating temp file
SaladRaider 54a9b78
Allow zipping of sub-dirs and add authentication
SaladRaider File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -99,3 +99,7 @@ ENV/ | |
|
||
# mypy | ||
.mypy_cache/ | ||
|
||
# osx | ||
*.DS_Store | ||
notebook.zip |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
sudo: false | ||
language: python | ||
python: | ||
- "3.5" | ||
install: pip install tox-travis | ||
script: tox |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,21 @@ | ||
# nbzip | ||
Zips and downloads all the contents of a jupyter notebook | ||
|
||
![nbzip demo](doc/demo.gif) | ||
|
||
# Installation | ||
|
||
There is no package on PyPI available right now. You can install directly from master: | ||
|
||
pip install git+https://github.com/data-8/nbzip | ||
|
||
|
||
You can then enable the serverextension | ||
|
||
jupyter serverextension enable --py nbzip --sys-prefix | ||
jupyter nbextension install --py nbzip | ||
jupyter nbextension enable --py nbzip | ||
|
||
# What is it? | ||
|
||
nbzip allows you to download all the contents of your jupyter notebook into a zipped file called 'notebook.zip'. |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
pip install --upgrade -e . | ||
jupyter serverextension enable --py nbzip | ||
jupyter nbextension install --py nbzip | ||
jupyter nbextension enable --py nbzip | ||
jupyter-notebook |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
from notebook.utils import url_path_join | ||
from .handlers import ZipHandler, UIHandler | ||
from tornado.web import StaticFileHandler | ||
|
||
import os | ||
|
||
# Jupyter Extension points | ||
def _jupyter_server_extension_paths(): | ||
return [{ | ||
'module': 'nbzip', | ||
}] | ||
|
||
def _jupyter_nbextension_paths(): | ||
return [{ | ||
"section":"tree", | ||
"dest":"nbzip", | ||
"src":"static", | ||
"require":"nbzip/tree" | ||
}] | ||
|
||
def load_jupyter_server_extension(nbapp): | ||
web_app = nbapp.web_app | ||
base_url = url_path_join(web_app.settings['base_url'], 'zip-download') | ||
handlers = [ | ||
(url_path_join(base_url, 'api'), ZipHandler), | ||
(base_url, UIHandler), | ||
( | ||
url_path_join(base_url, 'static', '(.*)'), | ||
StaticFileHandler, | ||
{'path': os.path.join(os.path.dirname(__file__), 'static')} | ||
) | ||
] | ||
web_app.add_handlers('.*', handlers) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
from tornado import gen, web, locks | ||
from notebook.utils import url_path_join | ||
from notebook.base.handlers import IPythonHandler | ||
from queue import Queue, Empty | ||
|
||
import traceback | ||
import urllib.parse | ||
import threading | ||
import json | ||
import os | ||
import jinja2 | ||
import zipfile | ||
|
||
TEMP_ZIP_NAME = 'notebook.zip' | ||
|
||
class ZipHandler(IPythonHandler): | ||
def __init__(self, *args, **kwargs): | ||
super().__init__(*args, **kwargs) | ||
|
||
def _emit_progress(self, progress): | ||
if isinstance(progress, Exception): | ||
self.emit({ | ||
'phase': 'error', | ||
'message': str(progress), | ||
'output': '\n'.join([ | ||
l.strip() | ||
for l in traceback.format_exception( | ||
type(progress), progress, progress.__traceback__ | ||
) | ||
]) | ||
}) | ||
else: | ||
self.emit({'output': progress, 'phase': 'zipping'}) | ||
|
||
@gen.coroutine | ||
def emit(self, data): | ||
if type(data) is not str: | ||
serialized_data = json.dumps(data) | ||
if 'output' in data: | ||
self.log.info(data['output'].rstrip()) | ||
else: | ||
serialized_data = data | ||
self.log.info(data) | ||
self.write('data: {}\n\n'.format(serialized_data)) | ||
yield self.flush() | ||
|
||
@gen.coroutine | ||
def get(self): | ||
try: | ||
base_url = self.get_argument('baseUrl') | ||
|
||
# We gonna send out event streams! | ||
self.set_header('content-type', 'text/event-stream') | ||
self.set_header('cache-control', 'no-cache') | ||
|
||
self.emit({'output': 'Removing old {}...\n'.format(TEMP_ZIP_NAME), 'phase': 'zipping'}) | ||
|
||
if os.path.isfile(TEMP_ZIP_NAME): | ||
os.remove(TEMP_ZIP_NAME) | ||
self.emit({'output': 'Removed old {}!\n'.format(TEMP_ZIP_NAME), 'phase': 'zipping'}) | ||
else: | ||
self.emit({'output': '{} does not exist!\n'.format(TEMP_ZIP_NAME), 'phase': 'zipping'}) | ||
|
||
self.emit({'output': 'Zipping files:\n', 'phase': 'zipping'}) | ||
|
||
q = Queue() | ||
def zip(): | ||
try: | ||
file_name = None | ||
zipf = zipfile.ZipFile(TEMP_ZIP_NAME, 'w', zipfile.ZIP_DEFLATED) | ||
for root, dirs, files in os.walk('./'): | ||
for file in files: | ||
file_name = os.path.join(root, file) | ||
q.put_nowait("{}\n".format(file_name)) | ||
zipf.write(file_name) | ||
zipf.close() | ||
|
||
# Sentinel when we're done | ||
q.put_nowait(None) | ||
except Exception as e: | ||
q.put_nowait(e) | ||
raise e | ||
|
||
self.gp_thread = threading.Thread(target=zip) | ||
self.gp_thread.start() | ||
|
||
while True: | ||
try: | ||
progress = q.get_nowait() | ||
except Empty: | ||
yield gen.sleep(0.5) | ||
continue | ||
if progress is None: | ||
break | ||
self._emit_progress(progress) | ||
|
||
self.emit({'phase': 'finished', 'redirect': url_path_join(base_url, 'tree')}) | ||
except Exception as e: | ||
self._emit_progress(e) | ||
|
||
|
||
class UIHandler(IPythonHandler): | ||
def initialize(self): | ||
super().initialize() | ||
# FIXME: Is this really the best way to use jinja2 here? | ||
# I can't seem to get the jinja2 env in the base handler to | ||
# actually load templates from arbitrary paths ugh. | ||
jinja2_env = self.settings['jinja2_env'] | ||
jinja2_env.loader = jinja2.ChoiceLoader([ | ||
jinja2_env.loader, | ||
jinja2.FileSystemLoader( | ||
os.path.join(os.path.dirname(__file__), 'templates') | ||
) | ||
]) | ||
|
||
@gen.coroutine | ||
def get(self): | ||
self.write( | ||
self.render_template( | ||
'status.html' | ||
)) | ||
self.flush() |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we have no intermediate page, what is this queue being used for? Am a bit confused :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i left it for logging, but I guess we don't really need it. I'll remove it.