Skip to content

Instantly share code, notes, and snippets.

@lbeltrame
Last active December 11, 2015 21:28
Show Gist options
  • Save lbeltrame/4662673 to your computer and use it in GitHub Desktop.
Save lbeltrame/4662673 to your computer and use it in GitHub Desktop.
PyQt4 + PyKDE4 script to auto-create IPython notebooks based on date, including starting the interpreter and browser (chromium). Depends upon having a "nbserver" profile set up in IPython. Depends upon: pathlib, psutil, PyQt4, PyKDE4, Pynotify.
#!/usr/bin/env python
from datetime import datetime
import sys
from IPython.nbformat import current
from pathlib import Path
from PyKDE4.kdecore import KStandardDirs
from PyKDE4.kio import KRun
import PyQt4.QtCore as QtCore
import PyQt4.QtGui as QtGui
import pynotify
import psutil
NOTEBOOK_DIR = Path("/path_to_notebooks/")
URL = "http://your-notebook-url-and-port/"
def ipython_pid():
for item in psutil.process_iter():
if "ipython" in item.name and "notebook" in item.cmdline:
return item.pid
def create_notebook():
name = datetime.now().strftime("%Y-%m-%d")
full_name = name + ".ipynb"
heading = "Current work - {}".format(name)
heading_cell = current.new_heading_cell(source=heading, level=2)
code_cell = current.new_code_cell()
cells = [heading_cell, code_cell]
worksheet = current.new_worksheet(cells=cells)
notebook = current.new_notebook(name, worksheets=[worksheet])
return notebook, full_name
class Runner(QtCore.QObject):
finished = QtCore.pyqtSignal()
def __init__(self, parent=None):
super(Runner, self).__init__(parent)
def launch_browser(self):
exe = KStandardDirs.findExe("chromium")
exe = exe.append(" %1").arg(URL)
result = KRun.runCommand(exe, None)
if not result:
notification = pynotify.Notification(
"Browser error",
"Chromium could not be started",
"dialog-error")
notification.show()
def run(self):
notebook, filename = create_notebook()
if not NOTEBOOK_DIR[filename].exists():
message = "Notebook {} created.".format(filename)
with NOTEBOOK_DIR[filename].open("w") as handle:
current.write(notebook, handle, "ipynb")
notification = pynotify.Notification("Notebook created", message,
"dialog-information")
notification.show()
else:
message = "Notebook {} already existing.".format(filename)
notification = pynotify.Notification("Notebook exists", message,
"dialog-warning")
notification.show()
if self.launch_ipython():
self.launch_browser()
self.finished.emit()
def launch_ipython(self):
pid = ipython_pid()
if pid is None:
exe = KStandardDirs.findExe("ipython")
exe.append(" notebook --profile nbserver")
result = KRun.runCommand(exe, None)
if not result:
message = "IPython cold not be started"
notification = pynotify.Notification("IPython error",
message, "dialog-error")
notification.show()
return False
return True
def main():
app = QtGui.QApplication(sys.argv)
pynotify.init("Notebook creator")
runner = Runner()
runner.finished.connect(app.quit)
QtCore.QTimer.singleShot(0, runner.run)
app.exec_()
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment