Skip to content

Instantly share code, notes, and snippets.

@technic
Created September 5, 2019 14:56
Show Gist options
  • Save technic/5c26a5fe8deacdb553825243b4581692 to your computer and use it in GitHub Desktop.
Save technic/5c26a5fe8deacdb553825243b4581692 to your computer and use it in GitHub Desktop.
Watch jupyter notebook changes and LiveReload slides
#!/usr/bin/env python
import sys
import os
import time
import logging
import argparse
import subprocess
import threading
import webbrowser
from livereload import Server
logger = logging.getLogger(__name__)
def update(filename):
command = ['jupyter-nbconvert', '--to', 'slides', filename]
logger.warning("Execute %s", " ".join(command))
subprocess.check_call(command)
if __name__ == "__main__":
logger.setLevel(logging.INFO)
parser = argparse.ArgumentParser(description="""
Watch jupyter notebook changes and nbconvert to reveal.js slides
Serve generated html with web server and LiveReload enabled (browser extension required)
""")
parser.add_argument("ipynb", type=str, help="jupyter notebook file")
args = parser.parse_args()
path = args.ipynb
assert os.path.isfile(path), "Can not open file '%s'" % path
update(path)
logger.info("Watching file %s", path)
server = Server()
server.watch(path, lambda: update(path))
def opener():
time.sleep(1)
base, _ = os.path.splitext(os.path.basename(path))
webbrowser.open('http://localhost:5500/%s.slides.html' % base)
threading.Thread(target=opener).start()
server.serve(root=os.path.dirname(path), liveport=35729)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment