How to use ContentsManager for Jupyter? - python

I've read about Jupyter ContentsManager but have no idea how to use it and the documentation is really bad. What should I do? Where do I run it and how do I connect it to my Jupyter Environment and notebooks?

To swap the contents manager in JupyterLab 3.0+ that is running on the new jupyter server (this is the default way of doing things, but if you are running on JupyterHub it might still use the old notebook server), create a jupyter_server_config.py file; you can auto-generate it in appropriate location using:
jupyter server --generate-config
and set the contents manager class to you own manager:
c.ServerApp.contents_manager_class = "python.module.for.your.ContentsManagerSubclass"
This option is described on the list of configuration options of jupyter server.
(alternatively, you could use a json file). For older JupyterLab versions, or if for some weird reason you are using the old notebook you will want to use jupyter_notebook_config.py where this option was named c.NotebookApp.contents_manager_class. You can read more on this topic here - although the examples reference the old notebook way of doing things so you would need to updated accordingly.
You may also be interested to learn how jupytext hot swaps the content managers reacting to user settings, see the code here.

Related

is it possible to hide kernels from the jupyterlab launcher?

I am working in Jupyterlab and I'm using voila to create dashboard application based on notebooks. Each aaplication lives withit's own set of dependancies thus a specific kernel (venv) is created for every single one of them.
My problem is that all these kernels are register in my jupyterlab env and displayed in the launcher as shown here:
I would like to hide them from jupyterlab so that they don't apear in the kernel list. Following this documentation (https://jupyter-notebook.readthedocs.io/en/stable/config.html) and other posts on SO, I created a notebook config file:
jupyter notebook --generate-config
and added this configuration to the file :
c.KernelSpecManager.whitelist = set('ir', 'javascript', 'python3')
but it didn't change anything to the launcher. Is there another way or did I miss something ?

Open IPython notebooks (*.ipynb) in read-only view (like a HTML file)

Nowadays with more and more IPython notebook files (*.ipynb) around, it is very disturbing every time when I want to peek at some notebook I have to open a server for it, and cannot do it in read-only mode. Due to auto-save I can accidentally change the file when reading it if not in read-only mode.
I hope something like this: ipython notebook mynb.ipynb --read-only would work, but sadly it doesn't (although still it creates a server which I don't really want in read-only view). What I really want is to open an ipynb file like a HTML file for reading; currently it seems a missing view of ipynb file, and now the notebook is more like a black-box or near-binary file alone.
(P.S. I am using Linux/Ubuntu.)
When you change the notebook files' permissions, jupyter's auto-save doesn't touch them:
chmod a-w *.ipynb
Then jupyter goes into read-only mode:
Try this ipynb Viewer. This renders ipython notebook as a static web-page.
Also ypu can convert ipyhton notebook to other formats using
ipython nbconvert --to FORMAT notebook.ipynb.
Refer Convert Ipython notebook to other formats. Using this you can convert ipython notebook to HTML.
You may also want to try nteract app (https://nteract.io)
nteract is a desktop application that allows you to develop rich documents that contain prose, executable code (in almost any language!), and images.
Here you can find more detailed review of the app.
It is completely free and very convenient tool by itself and I use it quite often to see other ipynb files if needed and for quick development.
The best I can suggest - unfortunately still a bit verbose - is using nbconvert to create a HTML version of the file, then opening it with your browser. Below are commands to do this (assuming that your browser is set up as the default program to handle .html files). Just replace yournotebook.ipynb with your real Notebook name.
Linux
jupyter nbconvert --to html yournotebook.ipynb --output /tmp/notebook.html &&
xdg-open /tmp/notebook.html
macOS
jupyter nbconvert --to html yournotebook.ipynb --output /tmp/notebook.html &&
open /tmp/notebook.html
Windows
jupyter nbconvert --to html yournotebook.ipynb --output "%TEMP%\notebook.html" && start "" "%TEMP%\notebook.html"
(Note that if you want to do this in bulk, perhaps in a loop in a script, you'll probably want to modify the commands above to not always use the same filename for the HTML file, to avoid the race condition where the HTML file has been overwritten by the time the browser actually gets round to starting to read it.)
Intellij IDEA (which is also available as free open-source community edition) can render ipynb files as well. In fact it also allows to author notebooks, so it's not just a viewer.
It can be used via file type associations or via the command line launcher (e.g. idea foo.pynb).
I use Github Gist for that. You'll need a Github account.
If you specify *.ipynb file extension there for your content copy/paste, it'll detect it and format as html view. You'll be able even sharing it with someone using link if that's interesting. No tools required other than having a browser.
I've found some glitches in formatting though, mostly with output plots, but it's reasonably rare.
Random example here: https://gist.github.com/Clockware/aa7e01722579841d5888ca83385a5f1d
I was able to read .ipynb files as html in visual code. You would need a python plugin for it which visual code auto detects. Fairly straight forward after that.
This most convenient method to take a peek at jupyter notebooks is by using nb-viewer, with this utility, you will be able to open notebooks (in read-only mode) with a double click.

How to place custom Jupyter kernels inside virtual environment?

I have a custom Jupyter kernel which runs IPython using a custom IPython profile which uses a matplotlib stylesheet.
I know to run this successfully normally I would put:
The matplotlib stylesheet in ~/.config/matplotlib/stylelib/
The IPython profile in ~/.ipython/
The kernel json in ~/.jupyter/kernels/my_kernel/
But I am doing this as part of larger program which runs in a virtualenv, and if I put the things as above then any notebook server running on the computer will be able to see the custom kernels, even if it is running outside the venv. I don't what this because I don't want my program to interfere with other notebooks on the computer.
I think what I need to do is put the things above somewhere equivalent inside the venv but I can't figure out where they should go. Doe anyone know where they would go? Or is this just a thing IPython/Jupiter can't/won't do?
It's probably worth mentioning that in the case of the stylesheet for example I don't want to just put it in the working directory of my program (which is one option matplotlib offers).
You can put kernelspecs in VIRTUAL_ENV/share/jupyter/kernels/ and they will be made available if the notebook server is running in that env. In general, <sys.prefix>/share/jupyter/kernels is included in the path to look for kernelspecs.
You can see the various locations Jupyter will look, you can see the output of jupyter --paths:
$ jupyter --paths
config:
/Users/you/.jupyter
/Users/you/env/etc/jupyter
/usr/local/etc/jupyter
/etc/jupyter
data:
/Users/you/Library/Jupyter
/Users/you/env/share/jupyter
/usr/local/share/jupyter
/usr/share/jupyter
runtime:
/Users/you/Library/Jupyter/runtime
Kernelspecs are considered data files, and will be found in any of those directories listed under data:, in a kernels subdirectory, e.g. /usr/local/share/jupyter/kernels.

Ipython installation - creating nbserver profile - missing notebook_config.py file

I am trying to use the guidance on http://www.slideshare.net/fullscreen/randyzwitch/ipython-ec2/12 to install public Ipython notebooks in an AWS instance. One problem that i encounter is, when i try to create a profile, i do not not observe the creation of an ipython_notebook_config_py file (as explained in the tutoral, as per the screenshot), but i only get a ipython_kernel_config.py file, which has very different contents, and cannot be edited in the way as explained in the tutoral. Can someone help me to understand why this happens, and what i should do subsequently? Many thanks.
The notebook server is no longer part of IPython; it's a separate Jupyter project, which has its own config directory ~/.jupyter. The config file for the notebook server is ~/.jupyter/jupyter_notebook_config.py. You can use this to configure the notebook server. If you want to keep multiple configurations of the notebook server, you can use the environment variable JUPYTER_CONFIG_DIR to specify that a different directory should be used:
JUPYTER_CONFIG_DIR=~/jupyter_nbserver jupyter notebook
Note: IPython has not lost its profiles or config files, so ~/.ipython/profile_default/ipython_config.py and startup files, etc. continue to work as before for configuring the IPython kernel, just not the notebook server itself.
References:
Running a public notebook server
Jupyter configuration
Migration from IPython to Jupyter

How do I get IPython profile behavior from Jupyter 4.x?

There was official(?) recommendation of running an IPython Notebook server, and creating a profile via
$ ipython profile create nbserver
as recommended in http://ipython.org/ipython-doc/1/interactive/public_server.html. This allowed for very different and very useful behavior when starting an IPython Notebook via ipython notebook and ipython notebook --profile=nbserver.
With Jupyter 4.0, there's a change and there are no longer profiles. I've found the conversation https://gitter.im/ipython/ipython/archives/2015/05/29 which has user minrk saying:
The .ipython directory has several things in it:
multiple config directories (called profiles)
one 'data' directory, containing things like kernelspecs, nbextensions
runtime info scattered throughout, but mostly in profiles
Jupyter follows more platform-appropriate conventions:
one config dir at JUPYTER_CONFIG_DIR, default: .jupyter
one data dir at JUPYTER_DATA_DIR, default: platform-specific
one runtime dir at JUPYTER_RUNTIME_DIR, default: platform-specific
And a rather cryptic remark:
If you want to use different config, specify a different config directory with JUPYTER_CONFIG_DIR=whatever
What's the best way to get different behavior (say, between when running as a server vs normal usage)?
Will it involve running something like:
$ export JUPYTER_CONFIG_DIR=~/.jupyter-nbserver
$ jupyter notebook
whenever a server 'profile' needs to be run? and
$ export JUPYTER_CONFIG_DIR=~/.jupyter
$ jupyter notebook
whenever a 'normal' profile needs to run? Because that seems terrible. What's the best way to do this in Jupyter 4.0?
Using some code from this blog post http://www.svds.com/jupyter-notebook-best-practices-for-data-science/ and updating it. The easiest solution appears to be to create an alias, like:
alias jupyter-nbserver='JUPYTER_CONFIG_DIR=~/.jupyter-nbserver jupyter notebook'
So now you can run the jupyter notebook with a different config via the simple command jupyter-nbserver.
A more robust solution might involve creating a bash function that changes the environment variable, checks whether there's a config file, if not creating one, then executing, but that's probably overkill. The answer that I give on this related question https://stackoverflow.com/a/32516200/246856 goes into creating the initial config files for a new 'profile'.

Categories

Resources