Open an ipython notebook via double-click on osx

余生长醉 提交于 2019-11-30 03:45:24

Use Pineapple application for opening and working on your IPython/Jupyter notebooks. It is pretty cool.

Update:

Now there is nteract, which is a new jupyter-like Desktop app. After installing it, make it the default app for opening .ipynb files. Then just double-click any notebook to start it right away.

I have found a nice way using Automator (Oct. 2017; with information from here):

  1. open Automator and create a new Application

  2. to add Run Shell Script drag and drop it from the list; might need these settings Shell: /bin/bash and Pass input: as arguments

  3. insert the code below; if necessary adjust the path to jupyter

Code

#!/bin/sh
variable="'$1'"
the_script='tell application "terminal" to do script "/usr/local/bin/jupyter notebook '
osascript -e "${the_script}${variable}\""
  1. save the script as an application (!)
  2. try to open a .ipynb file and change the default app to this newly created one.

note

This will open a terminal and run the jupyter notebook command, such that you can interrupt and stop the notebook-server from there. Also note that you cannot test the app like that in Automator, but need to add the Get Specified Finder Items and insert some test notebook there (just for testing purposes).

The application posted here worked pretty well for me: http://bioequity.org/2013/11/16/ipynbviewer/ You also need to download iTerm2, as described on that page.

Note that this doesn't work if there are spaces in the filename, but you can modify it so that it works with spaces in the filename. Control-click on the iPyNbViewer.app and select "Show package contents". Edit the file Contents/Resources/Scripts/main.scpt. Change three instances of "POSIX path" to "quoted form of POSIX path". Now it will work with spaces in the filename.

To set all of your .ipynb files to open with the app, you'll need to Get Info (command-I) on one of the files and select the iPyNbViewer app to open all .ipynb files.

It would be great if this was the default behavior of double-clicking on an iPython notebook file...

I came up with a way of doing it on Ubuntu. (It works for me but I can take no responsibility). It's explained here. In a nutshell, you need to create a new MIME type, then write a script that works as the app that launches them:

#!/bin/bash
netstat -tln |grep "8902"
# if not found - equals to 1, start it
if [ $? -eq 1 ]
then
ipython notebook / --no-browser --port=8902 &
sleep .5
fi
xdg-open http://localhost:8902/notebooks$1

This always opens the notebook server on port 8902, but first checks whether there is already a server running, and, if so, uses it. Then you can use ubuntu tweak to select your script as a default application for the MIME type "IPython Notebook" you just created. Not very elegant, but worth it, in my opinion.

Look at this link. Put a bash script in the folder where you keep your ipython notebooks and simply double click it to open up a notebook instance. From the link above, the bash script has just:

path=$0             # path is path to this file
cd ${path%/*.*}     # clip off the file name to get directory path and cd
ipython notebook --pylab inline

Finally, you need to chmod u+x the script to make it executable and you're done.

I have used the command line application 'nbopen' and put it in a Platypus wrapper to get drag'n drop and double click opening on Macos. 'nbopen' is downloadable using 'pip' It works well when used as described above by DanHickstein. Only problem with my code is that it requires the full path to the nbopen command. I know I should be able to use 'which nbopen' somehow but can't get it to work. Heres my Platypus code:

#!/bin/bash
#  Opens ipynb files in a Jupyter Notebook
#  echo $1
#  $1 is the path of the dropped file
/Users/robw/anaconda/bin/nbopen $1
#  Based on an idea from 
#  https://www.quora.com/Is-there-a-straightforward-way-to-open-an-IPython-Notebook-in-Windows-by-double-clicking
  1. pip install nbopen
  2. open Automator, create new Application - Add Run Shell Script (/bin/bash), Change Pass Input 'as arguments' - use this script:

    #!/bin/sh variable="'$1'" the_script='tell application "terminal" to do script "nbopen ' osascript -e "${the_script}${variable}\""

  3. copy the new application to "/Applications" directory

  4. goto any ipynb file and select the new app to open.

PyCharm now supports Jupyter ipynb files:

which is from the documentation https://www.jetbrains.com/help/pycharm/editing-jupyter-notebook-files.html.

But I think this feature is only available in the Professional version now; hopefully it will be added to the Community version in the future.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!