Skip to content

Commit

Permalink
ENH: Open a notebook from the command line
Browse files Browse the repository at this point in the history
This commit lets you open notebook files from the command line, just
like ipython can open (run) python files.  For example:

     ipython notebook foo.ipynb

Fixes ipython#945.
  • Loading branch information
punchagan committed May 7, 2012
1 parent 416a6b7 commit 1e72d29
Showing 1 changed file with 21 additions and 3 deletions.
24 changes: 21 additions & 3 deletions IPython/frontend/html/notebook/notebookapp.py
Expand Up @@ -257,6 +257,9 @@ class NotebookApp(BaseIPythonApplication):
# create requested profiles by default, if they don't exist:
auto_create = Bool(True)

# file to be opened in the notebook server
file_to_run = Unicode('')

# Network related information.

ip = Unicode(LOCALHOST, config=True,
Expand Down Expand Up @@ -392,6 +395,10 @@ def parse_command_line(self, argv=None):
# Kernel should inherit default config file from frontend
self.kernel_argv.append("--KernelApp.parent_appname='%s'"%self.name)

if self.extra_args:
self.file_to_run = os.path.abspath(self.extra_args[0])
self.config.NotebookManager.notebook_dir = os.path.dirname(self.file_to_run)

def init_configurables(self):
# force Session default to be secure
default_secure(self.config)
Expand Down Expand Up @@ -540,9 +547,20 @@ def start(self):
browser = webbrowser.get(self.browser)
else:
browser = webbrowser.get()
b = lambda : browser.open("%s://%s:%i%s" % (proto, ip, self.port,
self.base_project_url),
new=2)

if self.file_to_run:
filename, _ = os.path.splitext(os.path.basename(self.file_to_run))
for nb in self.notebook_manager.list_notebooks():
if filename == nb['name']:
url = nb['notebook_id']
break
else:
url = ''
else:
url = ''
b = lambda : browser.open("%s://%s:%i%s%s" % (proto, ip,
self.port, self.base_project_url, url),
new=2)
threading.Thread(target=b).start()
try:
ioloop.IOLoop.instance().start()
Expand Down

0 comments on commit 1e72d29

Please sign in to comment.