ArcadeDB, Ruby && Jupyter
Tags: arcadedb, ruby, iruby, jupyter
Published: 2024-01-19 07:08:31

Jupyter Notebooks are widely used in data science, scientific computing, and machine learning due to their interactive nature and ability to present code, results, and explanations in a clear and organized manner. The [IRuby Project](https://github.com/SciRuby/iruby) introduces a modern ruby kernel to jupyter and opens the environment for ruby projects.

[The ArcadeDB Ruby Interface](https://github.com/topofocus/arcadedb) naively supports iruby jupyter notebooks.

To activate include both gems in the Gemfile
```ruby
## Gemfile
gem 'arcadedb'
gem 'iruby'
```
Jupyter-output is realized through `IRuby.display, IRuby.html` and `IRuby.table`-commands in the `to_html`-method of the class to be supported. `ArcadeDB`-database objects inherit this `to_html`-implementation

```ruby
def to_html  # iruby
  the_rid = "[#{rid}] : {#{self.in.count}->}{->#{self.out.count }}"
  IRuby.display IRuby.html "<b>#{self.class}</b>#{the_rid} < #{html_attributes.map{|_,v|  v }.join(', ')} >"
end
```  
which leads to the following output

> * <p><tt>Member.find name: ‘Gross’ </tt><br /> 
<span style="color: #50953DFF"><b>Member</b>[#49:43] : {0-&gt;}{-&gt;0}</span> &lt; Frau, Maria, Gross, Lüneburg, Hauptstr. 12a &gt;</p>

### Customization

The default output is defined by 
```ruby
def html_attributes  # Standard
  invariant_attributes
end
```
It includes all properties except `created_at` and `modified_at`. Customization is straightforward:

```ruby
def html_attributes  # model/arcade/member.rb
 {     Sex: :sex,
   Vorname: :surname,
      Name: :name,
       Ort: :town,
    Straße: :street } 
end
```
This affects the output of tables, too. 
`Member.where` returns an array and triggers the expected jupyter-output:

* <p><tt>Member.where name: ‘Gross’ </tt><br /></p>
<hr />
<table><tbody>
                <tr><td>Sex</td><td>Vorname</td><td>Name</td><td>Ort</td><td>Straße</td></tr>
          <tr><td>Frau</td><td>Maria</td><td>Gross</td><td>Lüneburg</td><td>Hauptstr. 12a</td></tr></tbody></table>

### Advanced Visualization && Calculations

The ecosystem of applications supporting jupyter notebooks is slowly growing. Visualizations of any kind are realized with the excellent [vega](https://github.com/ankane/vega-ruby) gem. Jupyter notebooks are supported. 

ArcadeDB database contents are easily exported to [polars dataframes](https://github.com/ankane/polars-ruby). 

A typical workflow would be to use 
* [arcadedb](https://github.com/topofocus/arcadedb) to store and easily retrieve the data, 
* export them to [polars dataframes](https://github.com/ankane/polars-ruby), perform extensive calculations and use 
* [vega](https://github.com/ankane/vega-ruby) for visualization. 

The ruby script runs in a python environment, uses a java-based database for storage, a rust-powered Dataframe-Engine for calculations and a javascript library to present the results. 

<hr>

To experiment with this environment in jupyter, the [Github respiratory](https://github.com/topofocus/dev-adventures) from  the [Ruby Adventures with ArcadeDB Series](https://dev.to/topofocus/ruby-adventures-with-aradedb-45em) contains an `iruby` directory. 
Clone it, install `Jupyter` and ìruby`, go to the `iruby`-directory and enter `juypter notebook` to start Jupyter in the standard browser. Enjoy!