IPython kernels: specifying default directory in kernel.json - python

When defining my own custom ipython/jupyter kernels, is there a way to set a kernel specific default directory directly in the kernel.json file?
{
"argv": [ "python2", "-m", "IPython.kernel",
"-f", "{connection_file}"],
"display_name": "tmp_kernel",
"language": "python"
}
My goal is to create a kernel that per default creates its notebooks in my /tmp directory.
At the moment I am starting ipython in multiple terminals, which is rather inconvenient.

Related

Get conda environment variable inside Jupyter Notebook

With my conda environment activated I set an environment variable with the command
conda env config vars set ROOT_DIRECTORY=$PWD
Now, if a run echo $ROOT_DIRECTORY the output shows /home/augusto/myproject
How can I get that variable inside Jupyter Notebook? I tried with the command below, but the output shows None.
import os
print(os.getenv('ROOT_DIRECTORY'))
By the way, I have shure that Jupyter Notebook are using the correct Kernel. Running the above code inside a .py file works correctly, i.e. the output shows /home/augusto/myproject.
Find env dir
jupyter kernelspec list
cd path_to_kernel_dir
sudo nano kernel.json
Add environment section to json
json should look something like this
{
"argv": [
"/home/jupyter-admin/.conda/envs/tf/bin/python3",
"-m",
"ipykernel_launcher",
"-f",
"{connection_file}"
],
"env": {"LD_LIBRARY_PATH":"/home/jupyter-admin/.conda/envs/tf/lib/"},
"display_name": "tf_gpu",
"language": "python",
"metadata": {
"debugger": true
}
}

Is there any way to use jupyter notebook to run different python version in the same notebook?

I have added the conda environment to my Jupyter notebook, but the Python version is still 3.8 (Fig 1). What I would like to do is to create an environment which contains Python version 3.7 in a Jupyter notebook without starting from a command prompt and running two separate Jupyter notebooks (Fig 2). Is it possible to have simply one jupyter notebook with two separate environments and two different Python versions?
Fig1
Fig2
Please read this page of the docs for ipykernel.
In your environment you want to install only ipykernel (not full Jupyter), and use one of the ipykernel install --name command to register the kernels with Jupyter.
If that does not work, use jupyter kernelspec list to see which kernels jupyter see and where. A kernelspec is at minimum a kernel.json file in the right place to tell jupyter how to find kernels.
For example I have the following
$ cat ~/miniconda3/share/jupyter/kernels/python3/kernel.json
{
"argv": [
"~/miniconda3/bin/python",
"-m",
"ipykernel_launcher",
"-f",
"{connection_file}"
],
"display_name": "Python 3 (ipykernel)",
"language": "python",
"metadata": {
"debugger": true
}
}
I can use the documentation above, or create the following by hand:
$ cat ~/miniconda3/share/jupyter/kernels/python3.6/kernel.json
{
"argv": [
"~/miniconda3/envs/mypython36env/bin/python",
"-m",
"ipykernel_launcher",
"-f",
"{connection_file}"
],
"display_name": "Python 3.6 !!",
"language": "python",
"metadata": {
"debugger": true
}
}
and assuming I have the corresponding Python 3.6 env, then I'll get two kernel one of them being Python 3.6

Is there a way to set PYTHONHASHSEED for a Jupyter Notebook session?

I want to disable the randomization of Python's hash function. According to this thread (Unable to see or modify value of PYTHONHASHSEED through a module), setting PYTHONHASHSEED by setting os.environ['PYTHONHASHSEED'] at runtime does not work. How can I set PYTHONHASHSEED to a fixed value when I run Jupyter Notebook?
You can try to modify the kernel.json and add the line "env": {"PYTHONHASHSEED":"0"}.
The file is located in your python-folder or virtual environment at:
[venv|python-folder]/share/jupyter/kernels/python3/kernel.json
kernel.json:
{
"argv": [
"python",
"-m",
"ipykernel_launcher",
"-f",
"{connection_file}"
],
"display_name": "Python 3",
"language": "python",
"env": {"PYTHONHASHSEED":"0"}
}
That solves the issue for me.

VScode: Automating conversion of python file to Jupyter notebook with a task

In VScode I can write a python file with markdown and python cells and then convert it to a notebook via the command palette. Everything works well, but I would like to automate this with a task.
I know I could just define a shortcut for the conversion but then I would still need to manually save the notebook with the file explorer. Can I automate this with a task? If so how do I access the function for the conversion? Is this some internal function of VScode or can I access this function via the command line?
I tried a few things with the jupyter command in the command line but didn't have any luck. It seems like there is no command to convert a python file to a Jupyter notebook. Also I could not find a comprehensive documentation for the jupyter command.
Another question in regards to Jupyter notebooks in VScode: Is there a way how I can hide a cell from showing up? I know it would be possible if I edit the metadata of the notebook, but I hope there is a better way.
In case anyone else is still wondering about this, I'm now using jupytext for converting python files to jupyter notebooks.
I wrote two very simple bash scripts to automate the conversion and viewing of notebooks.
conversion:
#!/usr/bin/env bash
# retrieve file names and folder from arguments
full_fname=$1
fbase_name_no_ext=$2
dir_name=$3
workspace_folder=$4
full_path_no_ext="$dir_name/$fbase_name_no_ext"
notebook_save_path=$(cd ${dir_name}/.. && pwd)
# activate venv (venv should be in vscode workspace root)
source "${workspace_folder}/my_env/bin/activate"
echo "saving to: $notebook_save_path"
# convert to jupyter notebook
jupytext --to notebook ${full_fname}
# run all cells and save output to notebook file
jupyter nbconvert --to notebook --execute "${full_path_no_ext}.ipynb" --output "${notebook_save_path}/${fbase_name_no_ext}"
# cleanup intermediate notebook (contains only cells no output)
rm "${full_path_no_ext}.ipynb
viewing a notebook:
#!/usr/bin/env bash
# retrieve file names and folder from arguments
fbase_name_no_ext=$1
dir_name=$2
workspace_folder=$3
source "${workspace_folder}/my_env/bin/activate"
# notebooks are stored in the parent directory of the source file
notebook_folder=$(cd ${dir_name}/.. && pwd)
# view notebook in a browser window
jupyter notebook "${notebook_folder}/${fbase_name_no_ext}.ipynb"
Here is my vscode tasks file:
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "convert to NB",
"type": "shell",
"command": "${workspaceFolder}/convert",
"args": [
"${file}",
"${fileBasenameNoExtension}",
"${fileDirname}",
"${workspaceFolder}"
],
"group": {
"kind": "build",
"isDefault": true
},
"presentation": {
"echo": true,
"reveal": "always",
"focus": true,
"panel": "shared",
"showReuseMessage": true,
"clear": true
}
},
{
"label": "view NB",
"type": "shell",
"command": "${workspaceFolder}/viewNB",
"args": [
"${fileBasenameNoExtension}",
"${fileDirname}",
"${workspaceFolder}"
]
}
]
Since I'm no bash expert, there might be multiple things that could be done in a better way. Critique is always welcome!
Hope this is helpful to someone.

How to automatically load profile in IPython/Jupyter notebooks?

In earlier versions of IPython it was possible to load a specific profile by
ipython notebook --profile=my_profile
so that I can include things like autoreload in my_profile.
Since using IPython 4.0.1 (Jupyter actually) I'm getting the
[W 09:21:32.868 NotebookApp] Unrecognized alias: '--profile=my_profile', it will probably have no effect.
warning, and the profile is not loaded. Have you come across a workaround?
In Jupyter create a kernel for each profile.
Find the kernels directory under the jupyter configuration directory {jupyter configuration}/kernels (on my Mac this is $HOME/Library/Jupyter/kernels):
$ mkdir profile-x-kernel
$ cat << EOF > profile-x-kernel/kernel.json
{
"display_name": "Profile X (Python 3)",
"language": "python3",
"env": { },
"argv": [ "python3", "-m", "ipykernel", "--profile", "profile_x", "-f", "{connection_file}" ]
}
EOF
Then you can select the kernel from the kernel menu in Jupyter, without having to restart the whole notebook server.

Categories

Resources