Jupyter integration with emacs

I’ve mentioned in a thread or two that I’ve been using the excellent jupyter emacs package with Julia for several months now, so I thought it might be a good idea to make a topic to highlight the package. For me, this package has worked very well and replaced julia-repl as my REPL for julia within emacs and replaced ob-ipython and ein for notebook-style work in emacs (I never actually got ob-julia working, and it isn’t currently maintained).

The cool thing about this package is that it’s designed as a general-purpose interface to jupyter from within emacs which the author has simply built a REPL and org-mode integration on top of. This means that unlike ob-ipython and ein, it doesn’t make a lot of python-specific assumptions about jupyter. Further, the integrations are customizable per-language, and the author is actually a julia user, so out of the box the REPL has niceties like latex substitution, rich text formatting of docstring markdown, and the prompt actually changing to e.g. help?> when in package, help, or shell modes.

Anyway, thought it might be worth posting on a Julia tool for emacs that the community might not be very aware of.

15 Likes

It’s especially important that it gets linked to because it never shows up on google at least for me unless I include the author’s name. Not sure why that is.

1 Like

FWIW I googled “emacs jupyter” today and it comes up first (even in a private mode window).

1 Like

This is seriously cool. Anyone care to share their .emacs file for using this to provide interaction with .jl files?

1 Like

I had a few difficulties installing it, so maybe this write-up will be all the more useful.

Here is how things go if everything works well:

  1. Installation:

    1. Install IJulia:

       julia> using Pkg
       julia> Pkg.add("IJulia")
       julia> using IJulia
       julia> notebook()  # will install Jupyter the first time
       # Shutdown the notebook from the browser before quitting
       # the REPL
      
    2. Install the jupyter package from ELPA:

      M-x package-install RET jupyter RET

  2. Usage:

    1. Run a Julia REPL:

      M-x jupyter-run-repl RET

    2. Associate jour julia buffer(s) to the kernel:

      M-x jupyter-repl-associate-buffer RET

    3. You’re good to go, and benefit from a few features, among which:

      • C-M-x: evaluate the defun at point
      • C-cC-c: evaluate the line/region at point
      • C-cC-b: evaluate the whole buffer
      • M-i: get help for the thing at point

Here is what a small example session looks like:

  • I evaluated the function first using C-M-x
  • then the hello() call in the file using C-cC-c; this opened the “output” window at the bottom right
  • I manually typed hello() in the REPL (top right wndow)
  • I pressed M-i with point on println; this opened the bottom left help window.




Now, here are the issues I had:

  • "Modules are not supported" at step 2: this is because your Emacs should support dynamic modules. It wasn’t the case for my out-of-the-box Emacs version on Debian and I had to manually rebuild and reinstall an Emacs version configured using --with-modules.

  • "No kernelspecs available" at step 2: if, like me, you let IJulia install jupyter automagically for you, chances are that the jupyter command is not in your PATH. You’ll have to add the correct jupyter installation directory to your PATH for Emacs to be able to find it.

    • first, determine the correct directory by running a notebook:

        julia> using IJulia
        julia> notebook()
        [ Info: running setenv(`/home/francois/.julia/conda/3/bin/jupyter notebook`,["PATH=
        /home/francois/.julia/conda/3/bin:/home/francois/.scripts:/home/francois/.local/bin
        :/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games:/home/francois/.local/jul
        ia-1.1.0/bin/:/home/francois/projets/git/skeleton.jl/:/home/francois/.local/julia-1
        .1.0/bin/:/home/francois/projets/git/skeleton.jl/", ...
        ...])                               
      

      In this case, the directory we’re looking for is:
      /home/francois/.julia/conda/3/bin

    • then, add this directory to your PATH, either from your .bashrc file (or equivalent for your shell), or directly from Emacs:

        (setenv "PATH"
                (concat "/home/francois/.julia/conda/3/bin:" 
                        (getenv "PATH")))
8 Likes

Thanks for the guide @ffevotte.
I had to modify exec-path as well. (add-to-list 'exec-path “/home/dicker/.julia/conda/3/bin”)