Bending bits...

Bytes and Words...

Jupyter notebook using Org mode

Environment Setup

  1. Install miniconda or anaconda and create an environment.
  2. Install jupyter and ipykernel
  3. Run python -m ipykernel install --user --name kernel_name to create a custom kernel.

Emacs configuration

(use-package jupyter
    :ensure t)

(org-babel-do-load-languages
 'org-babel-load-languages
 '((emacs-lisp . t) ;; Other languages
   (shell . t)
   ;; Python & Jupyter
   (python . t)
   (jupyter . t)))

(org-babel-jupyter-override-src-block "python")
(setq ob-async-no-async-languages-alist '("python" "jupyter-python"))

(use-package conda
  :ensure t
  :config
  (setq conda-anaconda-home (expand-file-name "~/miniconda3/"))
  (setq conda-env-home-directory (expand-file-name "~/miniconda3/"))
  (setq conda-env-subdirectory "envs"))

(unless (getenv "CONDA_DEFAULT_ENV")
  (conda-env-activate "statistics"))

(defun my/jupyter-refresh-kernelspecs ()
  "Refresh Jupyter kernelspecs"
  (interactive)
  (jupyter-available-kernelspecs t))

(setq org-confirm-babel-evaluate nil)
(setq org-src-fontify-natively t)
(setq org-src-tab-acts-natively t)
(setq org-edit-src-content-indentation 0)
(org-babel-jupyter-override-src-block "python")

Need to mention that on Windows, I had to add at the start of configuration file:

(setenv "PATH"
        (concat
         "C:/miniconda3/Scripts;"
         "C:/miniconda3/condabin;"
         (getenv "PATH")))


(setq exec-path (append exec-path
                        '("C:/miniconda3/Scripts"
                          "c:/miniconda3/condabin")))

In the org file, simply add:

#+PROPERTY: header-args:python :kernel tutorials :async yes :results output :exports both
#+PROPERTY: header-args:python :session hello
#+PROPERTY: header-args:python+ :async yes

And use python :kernel tutorials :tangle "test.py" for each block of code.