Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated the README to simplify steps for new users #51

Merged
merged 3 commits into from
Feb 25, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
160 changes: 101 additions & 59 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@
Jupynium uses Selenium to automate Jupyter Notebook, synchronising everything you type on Neovim.
Never leave Neovim. Switch tabs on the browser as you switch files on Neovim.

<img src=https://user-images.githubusercontent.com/12980409/211894627-73037e83-4730-4387-827c-98ed2522740d.gif width=100% />

Note that it doesn't sync from Notebook to Neovim so only modify from Neovim.

### How does it work?
<img src=https://user-images.githubusercontent.com/12980409/211894627-73037e83-4730-4387-827c-98ed2522740d.gif width=100% />

## How does it work?

<img src=https://user-images.githubusercontent.com/12980409/211933889-e31e844c-1cf3-4d1a-acdc-70cb6e244ee4.png width=50% />

The Jupynium server will receive events from Neovim, keep the copy of the buffer and apply that to the Jupyter Notebook by using Selenium browser automation. It interacts only through the front end so it doesn't require installing extensions on the kernel etc., which makes it possible to
The Jupynium server will receive events from Neovim, keep the copy of the buffer and apply that to the Jupyter Notebook by using Selenium browser automation. It interacts only through the front end so it doesn't require installing extensions on the kernel etc., which makes it possible to:

- Develop locally, run remotely (or vice versa)
- Use university-hosted Jupyter Notebook
Expand All @@ -29,27 +29,35 @@ The Jupynium server will receive events from Neovim, keep the copy of the buffer
### Requirements

- 💻 Linux, macOS and Windows (CMD, PowerShell, WSL2)
- 🦊 Firefox (Other browsers are not supported due to their limitation with Selenium)
- 🦎 Mozilla geckodriver (May be automatically installed with Firefox. Check `geckodriver -V`)
- ✌️ Neovim >= v0.8
- 🦊 Firefox
- Other browsers are not supported due to their limitation with Selenium (see [#49](https://github.com/kiyoon/jupynium.nvim/issues/49#issuecomment-1443304753))
- 🦎 Mozilla geckodriver
- May already be installed with Firefox. Check `geckodriver -V`
- 🐍 Python >= 3.7
- 📔 Jupyter Notebook >= 6.2 (Doesn't support Jupyter Lab)
- Supported Python installation methods include system-level and [Conda](https://docs.conda.io/en/latest/miniconda.html)
- 📔 Jupyter Notebook >= 6.2
- Jupyter Lab is not supported

### Install Python

Don't have system python 3.7? You can use [Conda](https://docs.conda.io/en/latest/miniconda.html).
Don't have system Python 3.7? You can use [Conda](https://docs.conda.io/en/latest/miniconda.html):

```bash
conda create -n jupynium python=3
conda activate jupynium
```

Upgrade pip. This solves many problems.
Upgrade pip. This solves many problems:

```bash
# pip >= 23.0 recommended
pip3 install --upgrade pip
```

Install with vim-plug:
### Install Jupynium

With vim-plug:

```vim
Plug 'kiyoon/jupynium.nvim', { 'do': 'pip3 install --user .' }
Expand All @@ -58,7 +66,7 @@ Plug 'rcarriga/nvim-notify' " optional
Plug 'stevearc/dressing.nvim' " optional, UI for :JupyniumSelectKernel
```

Install with packer.nvim:
With packer.nvim:

```lua
use { "kiyoon/jupynium.nvim", run = "pip3 install --user ." }
Expand All @@ -67,7 +75,7 @@ use { "rcarriga/nvim-notify" } -- optional
use { "stevearc/dressing.nvim" } -- optional, UI for :JupyniumSelectKernel
```

Install with 💤lazy.nvim
With 💤lazy.nvim:

```lua
{
Expand All @@ -80,7 +88,7 @@ Install with 💤lazy.nvim
"stevearc/dressing.nvim", -- optional, UI for :JupyniumSelectKernel
```

Setup is optional for system python users and here are the defaults. Conda users need to change the `python_host`.
The default configuration values are below and work well for system-level Python users. If you're a Conda user, you may need to change `python_host` to execute using the `conda` command instead.

<details>
<summary>
Expand Down Expand Up @@ -180,66 +188,108 @@ require("jupynium").setup({

</details>

## 🏃 Quick Start

- Open a `*.ju.py` file.
- Execute `:JupyniumStartAndAttachToServer`. This will open Jupyter Notebook on the Firefox browser.
- If not, clarify option `jupyter_command` or just open the Notebook server by yourself: `jupyter notebook`
- Execute `:JupyniumStartSync`. This will create an `Untitled.ipynb` file on the browser.
- Now you can type `# %%` in Neovim to create a code cell.
- You'll see everything you type below that will be synchronised in the browser.
- Execute cells using the default keybind `<space>x`.

For detailed instructions, see Usage.

## 🚦 Usage

First, I recommend setting password on your notebook (rather than using tokens)
There are 2 general steps to using Jupynium:

1. Setup a Jupynium file
2. Connect to the Jupynium server

The Jupynium server stays alive as long as the browser is alive. So you can see them as the same thing in this doc.
For example:

- Starting Jupynium server = opening a Selenium browser
- Manually closing the browser = closing the Jupynium server

### Setup a Jupynium file

Jupynium uses a unique file format (see the `Jupynium file format` section below). This `.ju.py` file is what you will primarily be interacting with, rather than the `.ipynb` file directly. The contents of the Jupynium file are synced to the browser notebook where it can be viewed in real-time. If you want to keep a copy of the notebook, it can be downloaded as an `.ipynb` file later.

First, it's recommended to set a password on your notebook (rather than using tokens):

```console
$ jupyter notebook password
Enter password: 🔒

$ jupyter notebook # leave notebook opened
$ jupyter notebook # leave notebook opened
```

**Jupynium server stays alive as long as the browser is alive.**
So you can see them as the same thing in this doc.
For example:
#### If you want to start a new notebook

- Starting Jupynium server = opening a Selenium browser
- Manually closing the browser = closing the Jupynium server
1. Manually create a local Jupynium file called `<filename>.ju.py`
2. Done! The rest happens after connecting to the server

#### If you want to open an existing notebook

There are currently 2 ways of converting an existing `.ipynb` file to a Jupynium file:

**Option 1**: Use an included command line tool:

```bash
ipynb2jupy [-h] [--stdout] file.ipynb [file.ju.py]
```

### Open and attach to a Jupynium server
**Option 2**: This method requires that you have already connected to the Jupynium server:
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You mentioned about "Jupynium server" without explaining what it is. It's in the next section that you can think of it as a selenium browser session, but I think we should mention that first at the very beginning of the Usage section


**This is for local neovim only. For remote neovim, see [Command-Line Usage](#%EF%B8%8F-command-line-usage-attach-to-remote-neovim).**
1. Open your `.ipynb` file in the web browser after connecting to the server
2. In a new Neovim buffer, run `:JupyniumLoadFromIpynbTab`. This will convert the contents of the notebook file to Jupynium format.
3. Save your buffer as `<filename>.ju.py`

Running `:JupyniumStartAndAttachToServer` will open the notebook.
Type password and once **you need to be on the main page (file browser) for the next steps**.
When using Jupynium for the first time, it's recommended to start a new notebook to make sure everything works before trying to load existing files.

**New in 0.1.1:**
Jupynium will open Jupyter Notebook server for you if not found.
It will also open the ipynb file in the current directory and ask you if you want to sync from vim or from ipynb.
### Connect to the Jupynium server

### Sync current buffer to the Jupynium server
(This is for local Neovim only. For remote Neovim, see [Command-Line Usage](#%EF%B8%8F-command-line-usage-attach-to-remote-neovim))

You attached your nvim instance to the server, but it won't automatically start syncing.
Run `:JupyniumStartSync` to create a new ipynb file and start syncing from vim to notebook.
In Neovim, with your Jupynium `.ju.py` file open, you can run `:JupyniumStartAndAttachToServer` to start the notebook server.

#### Sync current buffer to the Jupynium server

You need to be on the main notebook page (file browser) for the next few steps.

Although Neovim is now attached to the server, it won't automatically start syncing.

To sync your Neovim Jupynium file to a notebook, run `:JupyniumStartSync`.

You can also:

1. `:JupyniumStartSync filename` to give name (`filename.ipynb`) instead of `Untitled.ipynb`.
- `:JupyniumStartSync filename` to give a name to the notebook (`filename.ipynb`) instead of `Untitled.ipynb`. This does not open existing files. If a file with that name already exists then the filename argument will just be ignored.
- To sync a Jupynium file to an existing notebook, manually open the file in the browser, and `:JupyniumStartSync 2` to sync to the 2nd tab (count from 1).

- This will not open a file if it exists. Instead filename will be ignored.
At this point, any changes you make within the Neovim Jupynium file will be reflected live in the browser. Make sure you do not make changes inside the browser itself, as the sync is only one-way (from Neovim to browser).

2. Manually open the file from the browser, and `:JupyniumStartSync 2` to sync using the 2nd tab (count from 1).
If you want to save a copy of the `.ipynb` file, run `:JupyniumDownloadIpynb`. There is also a configuration option to enable automatic downloading.

### Use multiple buffers
#### Sync multiple Jupynium files

Run `:JupyniumStartSync` again with a new buffer you want to sync.
You can sync multiple files at the same time. Simple run `:JupyniumStartSync` again with the new file you want to sync.

### Use multiple neovim
#### Use multiple Neovim

You can run `:JupyniumStartSync` on a new neovim instance.
You can run `:JupyniumStartSync` on a new Neovim instance.
If you have `auto_attach_to_server = false` during setup, you need to run `:JupyniumAttachToServer` and `:JupyniumStartSync`.

## 📝 .ju.py (or .ju.\*) file format
## 📝 Jupynium file format (.ju.py or .ju.\*)

The file format is designed to be LSP friendly even with markdown code injected in it. The markdown cells will be part of python string `"""%%` ... `%%"""`.
The file format is designed to be LSP friendly even with markdown code injected into it. The markdown cells will be part of a Python string: `"""%%` ... `%%"""`.

**Code cell separators:**
i.e. Any code below this line (and before the next separator) will be a code cell.

- `# %%`: recommended
- `%%"""`: use when you want to close markdown cell
- `%%"""`: use when you want to close a markdown cell
- `%%'''`

**Markdown cell separators:**
Expand Down Expand Up @@ -288,7 +338,7 @@ If you want custom keymaps, add `textobjects = { use_default_keybindings = false
## 📡 Available Vim Commands

```vim
" Server (only used when neovim is local. See Command-Line Usage for remote neovim)
" Server (only used when Neovim is local. See Command-Line Usage for remote neovim)
:JupyniumStartAndAttachToServer [notebook_URL]
:JupyniumAttachToServer [notebook_URL]

Expand Down Expand Up @@ -320,7 +370,7 @@ If you want custom keymaps, add `textobjects = { use_default_keybindings = false
:JupyniumShortsightedToggle
```

### Lua API
## Lua API

The core API is provided as a global function.

Expand All @@ -346,35 +396,33 @@ if status_ok then
end
```

## 👨‍💻️ Command-Line Usage (Attach to Remote NeoVim)
## 👨‍💻️ Command-Line Usage (attach to remote Neovim)

**You don't need to install the vim plugin to use Jupynium.**
**The plugin is responsible of adding `:JupyniumStartAndAttachToServer` etc. that just calls the command line program,**
**plus it has textobjects and shortsighted support.**
**You don't need to install the vim plugin to use Jupynium.** The plugin is responsible of adding `:JupyniumStartAndAttachToServer` etc. that just calls the command line program, plus it has textobjects and shortsighted support.

Install Jupynium if you haven't.
Install Jupynium if you haven't already:

```bash
$ pip3 install jupynium
pip3 install jupynium
```

Open a python/markdown file with nvim and see `:echo v:servername`.
Run Jupynium like:

```bash
$ jupynium --nvim_listen_addr /tmp/your_socket_path
jupynium --nvim_listen_addr /tmp/your_socket_path
```

Or, you can run Neovim like

```bash
$ nvim --listen localhost:18898 notebook.ju.py
nvim --listen localhost:18898 notebook.ju.py
```

Then start Jupynium as well as attaching the neovim to it.

```bash
$ jupynium --nvim_listen_addr localhost:18898
jupynium --nvim_listen_addr localhost:18898
```

Note that you can attach to remote neovim by changing `localhost` to `servername.com` or using SSH port forwarding.
Expand All @@ -385,19 +433,13 @@ Additionally,

- You can run `jupynium` command multiple times to attach more than one Neovim instance.
- `jupynium --notebook_URL localhost:18888` to view different notebook.
- You can just run `jupynium` without arguments to just let the server / browser running and wait for nvim to attach.

### Converting ipynb to Jupynium file

```bash
ipynb2jupy [-h] [--stdout] file.ipynb [file.ju.py]
```
- You can just run `jupynium` without arguments to just leave the server / browser running and wait for nvim to attach.

## ⚠️ Caution

The program is in the alpha stage. If it crashes it's likely that the whole browser turns off without saving!

#### Rules:
### Rules

1. Always leave the home page accessible. Jupynium interacts with it to open files. Do not close, or move to another website.

Expand Down