Skip to content

Commit

Permalink
Julia jupyter interpreter is up!
Browse files Browse the repository at this point in the history
Though it's not as simple as other interpreters...
(The kernel has to be manually started and stopped)
  • Loading branch information
michaelb committed May 21, 2021
1 parent 18be53e commit 148bbbf
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 60 deletions.
16 changes: 8 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ An example in C, look in the command area:

![](ressources/visual_assets/demo_c.gif)

##### The result can be displayed in multiple (even at the same time) ways:
##### The result can be displayed in multiple (even at the same time) ways:

[Classic](ressources/display_classic.md)| [Virtual Text](ressources/display_virtualtext.md)
:------------------------------------------:|:------------------:
Expand Down Expand Up @@ -95,7 +95,7 @@ Basically, it allows you to run a part of your code.
Do either of:

- Position the cursor on a line `:SnipRun`
- Select some visual range, `:'<,'>SnipRun`
- Select some visual range, `:'<,'>SnipRun`
- Combine a motion with the operator

(preferably through a shortcut!)
Expand All @@ -111,7 +111,7 @@ Sniprun will then:
- **Add boilerplate** when it exists. In C, it surrounds your snip with "int main() {", "}". (disclaimer: oversimplifed)
- **Build** (write to a script file, or compile) the code
- **Execute** the code
- Return stdout, or stderr using the
- Return stdout, or stderr using the


![](ressources/visual_assets/760091.png)
Expand Down Expand Up @@ -275,7 +275,7 @@ require'sniprun'.setup({
-- "LongTempFloatingWindow", -- "same as above, but only long results. To use with VirtualText__
-- "Terminal" -- "display results in a vertical split
},
-- customize highlight groups (setting this overrides colorscheme)
snipruncolors = {
SniprunVirtualTextOk = {bg="#66eeff",fg="#000000",ctermbg="Cyan",cterfg="Black"},
Expand Down Expand Up @@ -387,15 +387,15 @@ println!("-> {}", alphabet);
| ------------ | ------------- | --- | ---------- | ---------------- |
| Ada | Line | | Java | Bloc |
| Bash/Shell | Bloc + REPL\* | | JavaScript | Bloc |
| C | Import | | Julia | Bloc |
| C | Import | | Julia | Bloc + REPL\*\* |
| C++ | Import | | Lisp | Untested |
| Clojure | Untested | | Lua | Bloc |
| COBOL | Untested | | Lua-nvim | Bloc |
| Coffeescript | Bloc | | Markdown (GFM) | Bloc + REPL \*** |
| Coffeescript | Bloc | | Markdown (GFM) | Bloc + REPL \*\*\* |
| C# | Untested | | Perl6 | Line |
| D | Bloc | | Perl | Line |
| Elixir | Untested | | PHP | Untested |
| Elm | Untested | | Python3 | Import +REPL\*\* |
| Elm | Untested | | Python3 | Import +REPL*\* |
| Erlang | Untested | | R | Bloc + REPL \*\* |
| F# | Untested | | Ruby | Bloc |
| Go | Bloc | | Rust | Bloc |
Expand Down Expand Up @@ -425,7 +425,7 @@ Due to its nature, Sniprun may have trouble with programs that :

## Troubleshooting

begin by updating the plugin and running `:SnipReset` and then `:checkhealth sniprun`
begin by updating the plugin and running `:SnipReset` and then `:checkhealth sniprun`

- **Silent fail**: the sniprun binary may be incompatible with your distro/OS/arch. Use `bash ./install.sh 1` as post-install to compile locally.
- Terminal and Floating Window display mode do not work: Linked to [this](https://github.com/michaelb/sniprun/issues/70) issue, no fix found yet.
Expand Down
12 changes: 12 additions & 0 deletions doc/Julia_jupyter.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
The setup for the julia_jupyter interpreter is quite convoluted:

Indeed, the Julia jupyter kernel MUST be started before Sniprun can run (and there is even a consequent delay since the kernel is so slow to start).

You should start a julia jupyter kernel with the following command:
` jupyter-kernel --kernel=julia-1.5 --KernelManager.connection_file=$HOME/.cache/sniprun/julia_jupyter/kernel_sniprun.json`

(adapt to your XDG_CACHE location if you're on Mac)

You manage kernel startup AND shutodwn manually. Why? There is a terrible data race if sniprun does it. Python_jupyter works, julia doesn't. That's about it.

If you want to use another kernel location, post a feature request at github.com/michaelb/sniprun and i'll see what I can do.
55 changes: 3 additions & 52 deletions src/interpreters/Julia_jupyter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,28 +123,10 @@ impl Interpreter for Julia_jupyter {
impl ReplLikeInterpreter for Julia_jupyter {
fn fetch_code_repl(&mut self) -> Result<(), SniprunError> {
self.fetch_code()?;
let saved_code = self.read_previous_code();
if saved_code.is_empty() {
//initialize kernel. Relying on self.read_previous_code to
//know when to start a new kernel is important as
//this will be cleared by the SnipReplMemoryClean command
let _res = std::fs::remove_file(&self.kernel_file);
let _res = Command::new("jupyter-kernel")
.arg("--kernel=julia-1.5")
.arg(String::from("--KernelManager.connection_file=") + &self.kernel_file)
.spawn();
info!("Initialized kernel at {}", self.kernel_file);
} else {
// kernel already running
info!(
"Using already loaded jupyter kernel at {}",
self.kernel_file
);
if !std::path::Path::new(&self.kernel_file).exists() {
info!("no kernel file found");
return Err(SniprunError::RuntimeError("No running kernel found".to_string()));
}

// save kernel
self.save_code(saved_code);

Ok(())
}
fn add_boilerplate_repl(&mut self) -> Result<(), SniprunError> {
Expand Down Expand Up @@ -186,15 +168,6 @@ impl ReplLikeInterpreter for Julia_jupyter {
}

fn execute_repl(&mut self) -> Result<String, SniprunError> {
if !std::path::Path::new(&self.kernel_file).exists() {
info!("no kernel file found");
return Err(SniprunError::RuntimeError("No kernel found or provided".to_string()));
}
info!(
"json kernel file exists yet? {}",
std::path::Path::new(&self.kernel_file).exists()
);

info!("starting executing repl: bash {}",&self.launcher_path);
let output = Command::new("bash")
.arg(&self.launcher_path)
Expand Down Expand Up @@ -234,25 +207,3 @@ impl ReplLikeInterpreter for Julia_jupyter {
}
}
}

#[cfg(test)]
mod test_julia_jupyter {
use super::*;
use crate::*;

#[test]
fn run_all() {
simple_print();
}

fn simple_print() {
let mut data = DataHolder::new();
data.current_bloc = String::from("println(\"a\")");
let mut interpreter = Julia_jupyter::new(data);
let res = interpreter.run_at_level(SupportLevel::Bloc);

// should panic if not an Ok()
let string_result = res.unwrap();
assert!(string_result.contains(&"a"));
}
}

0 comments on commit 148bbbf

Please sign in to comment.