Skip to main content

ipyrust

Simple and hackable jupyter kernel for running rust codes inside a python notebook.

Rather than providing a true interactive experience, this kernel will just extract the rust code from the notebook, compile it on the fly and print the result. It is meant as a drop-in replacement for your main rust file.

Install with:

pip install ipyrust

The package itself is in pure python, and will just call the cargo compiler already installed in your system. Of course, you have to install cargo in order to make it work. Other build tools should work as well.

Motivation

Other packages, such provide a better interactive experience when working with cpp, yet they introduce a lot of magic behind the scene. While developing algorithms with rust, you usually care more about speed and low level control. This package guarantees no overhead - it just transpiles your notebook into a plain .rs file: you may even check the result by yourself. Moreover, since this package is just a ~300lines pure python script, it's way easier to setup.

Usage

Note: you may find a complete example here, example.ipynb

Create a python notebook in the same folder of your Cargo.toml file. Done! Test your configuration by adding an hello world cell such as:

fn main() {
  println!("Hello, world!");
}

If you wish, you may override the default configuration using comments.

  • $$ipyrust_file: this is the location where your code would be extracted. Defaults to src/main.rs
  • $$ipyrust_build: this is the command ipyrust should use to pre-compile your code before running it. Default to none.
  • $$ipyrust_run: this is the command ipyrust should use to compile your code. Default to cargo run -q (q is the "quiet" flag)

For example:

// $$ipyrust_file: src/main.rs
// $$ipyrust_run: cargo run


fn main() {
  println!("Hello, world!");
  // this will print full output from cargo
}

Special formatting

You may provide non-textual data to the notebook, such as html or images, by printing some special tags. This feature is enabled by default, and let you create interactive notebooks.

disable

To disable any kind of special data for the rest of the cell, just print $$$ipykernelr_disable_special_output$$$ at the beginning of your main.

For example:

println!("$$$ipykernelr_disable_special_output$$$");

In this way, you will be sure that any further output will be printed "as is".

images or other files

You may display an image (or another kind of file) by printing the special tag $$$ipykernelr_file$$$ followed by the image path. ipycpp will guess the kind of file from the extension. For example:

println!("$$$ipykernelr_file$$$myfolder/myimage.png");

html

Mark html regions with $$$ipykernelr_html_start$$$ and $$$ipykernelr_html_end$$$ (newline required). You may also add some javascript logic to create interactive widgets.

Basic example:

const HTML : &str = r##"<b>some html</b>"##;

fn main() {
  println!("$$$ipykernelr_html_start$$$\n{HTML}\n$$$ipykernelr_html_end$$$\n");
}

Advanced example (this will create an interactive widget using the PetiteVue library; the widget will display a number and two buttons to increment or decrement it):

// widget example

const HTML : &str = r##"

  <div class="widgetcontainer">
    <div class="widget" v-scope="{ count: 0 }">
      <button @click="count--">-</button>
      {{ count }}
      <button @click="count++">+</button>
    </div>
  
    <script>
    if(!window.INSTALL_PETITE_VUE){
      let resolve = null;
      window.INSTALL_PETITE_VUE = new Promise(r => resolve = r);
      function loadPetiteVue(){
        var script = document.createElement('script');
        script.src = 'https://unpkg.com/petite-vue';
        script.onload = resolve;
        script.onerror = () => setTimeout(loadPetiteVue, 1000)
        document.head.appendChild(script);
      }
      loadPetiteVue()
    }
  
    {
      // get current element right now, and mount it as soon as petite-vue is loaded
      let element = document.currentScript.previousElementSibling;
      window.INSTALL_PETITE_VUE.then(() => PetiteVue.createApp().mount(element));
    }
  
    </script>
  </div>
"##;

fn main() {
  println!("$$$ipykernelr_html_start$$$\n{HTML}\n$$$ipykernelr_html_end$$$\n");
}

Authors and license

Main author: Luca Fabbian luca.fabbian.1999@gmail.com

Freely distributed under MIT license.

Feel free to open a github issue to report bugs or submit feature requests!

Release files for ipyrust 0.0.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for ipyrust 0.0.1
File Size Uploaded
ipyrust-0.0.1.tar.gz 6.4 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for ipyrust 0.0.1
File Interpreter ABI Platform
ipyrust-0.0.1-py3-none-any.whl Python 3 none any Details

Total release size: 12.8 kB

Release files / ipyrust-0.0.1.tar.gz

Download URL ipyrust-0.0.1.tar.gz
Size 6.4 kB
Tags Source
SHA-256 checksum
How to use checksums
5f83eb4000e41be41abbb913b1733feb5f962122f8d8ade762029867d607c68e
BLAKE2b-256 checksum
How to use checksums
8571cc731c64edff0d175f6641a13fcbc561e279587b982c6e34d32eb86a7c43
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.10.6

Release files / ipyrust-0.0.1-py3-none-any.whl

Download URL ipyrust-0.0.1-py3-none-any.whl
Size 6.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
d20532ba3c6b7294bc20d17923ed17a466d080a3adaa9e85ae10b10339f23c8b
BLAKE2b-256 checksum
How to use checksums
e1d919153fd991b358dc46fda0704346107729f45fc5cdb10def61b654b11dd7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.10.6

Release history Release notifications | RSS feed

This release

0.0.1 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page