Visualizing Data in dotnet with Polyglot Notebooks and SandDance
Tags: datavisualization, dotnet, vscode, dataviz
Published: 2023-05-13 02:55:33

---
title: "Visualizing Data in dotnet with Polyglot Notebooks and SandDance"
published: true
canonical_url: https://accessibleai.dev/post/polyglot_sanddance/
description: SandDance lets you make amazing interactive data visualizations in your Polyglot Notebooks. Here's how it works.
tags: DataVisualization,Dotnet,VSCode,DataViz
series: Polyglot Notebooks
cover_image: https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9mntw0kc3qi612tzuhmb.png
# cover_image: https://direct_url_to_image.jpg
# Use a ratio of 100:42 for best results.
# published_at: 2023-05-13 02:51 +0000
---

As I've been doing more and more [dotnet development in notebooks with Polyglot Notebooks](https://accessibleai.dev/post/introducing_polyglot_notebooks/) I've found myself wanting more options to create rich data visualizations inside of VS Code the same way I could use Plotly for data visualization using Python and Jupyter Notebooks.

Thankfully, [Microsoft SandDance](https://www.microsoft.com/en-us/research/project/sanddance/) exists and fills some of that gap in terms of doing rich data visualization from dotnet code.

In this article I'll talk more about what SandDance is, show you how you can use it inside of a Polyglot Notebook in VS Code, and show you a simple way you can use it without needing a Polyglot Notebook.

## What is SandDance?

SandDance is a [Microsoft Research project](https://www.microsoft.com/en-us/research/project/sanddance/) designed to fluidly visualize data in an interactive manner. SandDance supports different visualization types and aggregating, sorting, and colorizing data points to help the user perform complex data analysis.

![SandDance](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2rfjnwuassnaidhud0d8.png)

SandDance can be integrated into a variety of different environments from a [web application](https://microsoft.github.io/SandDance/app/) to a [Power BI integration](https://appsource.microsoft.com/en-US/product/power-bi-visuals/WA200000430), but most interesting to me is the extension to Polyglot Notebooks that allows you to directly pipe any data source you want into SandDance for analysis.

{% embed https://youtu.be/QQmEtdbPXeE %}

## Loading Data in Polyglot Notebooks

In order to work with SandDance, we'll need to install the `SandDance.InteractiveExtension` NuGet package. Along with that we'll install a few other extensions that make it easier to load and work with tabular data.

```
#r "nuget:SandDance.InteractiveExtension,*-*"
#r "nuget:DataView.InteractiveExtension,*-*"
#r "nuget:Microsoft.ML.DataView"
#r "nuget:Microsoft.Data.Analysis"
```

Once these NuGet packages are downloaded and installed into the notebook you can start loading your data with a C# code cell:

```csharp
using System.IO;
using System.Collections.Generic;
using Microsoft.Data.Analysis;
using Microsoft.ML;

// Load a CSV file into a DataFrame
string contents = File.ReadAllText("Titanic.csv");
DataFrame ratings = DataFrame.LoadCsvFromString(contents);

// Display the first 3 rows of the DataFrame below the cell
ratings.Head(3)
```

This will load the entire CSV file into an interactive `DataFrame` and then display the `DataFrame` below the cell as shown below:

![DataFrame](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qf2k07mlj3bry89c2dil.png)

The `Microsoft.Data.Analysis.DataFrame` class is one I want to explore at more length, but at early glance it appears to be very analogous to a Pandas DataFrame in Python.

## Sending Data to SandDance

Once you have a `DataFrame`, you can convert it to a `TabularDataResource` and then call the `ExploreWithSandDance` extension method to begin visualizing your data.

```csharp
using Microsoft.DotNet.Interactive.Formatting.TabularData;

TabularDataResource tabular = ratings.ToTabularDataResource();
SandDanceDataExplorer explorer = tabular.ExploreWithSandDance();
explorer.Display()
```

This will open SandDance immediately below the cell using the data source you provided.

![SandDance in a Polyglot Notebook](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vzoxsjgo5asosl8vlqbb.png)

The user experience in Polyglot with SandDance currently feels a bit cramped to me and I do not know of an easy way of maximizing the SandDance output to a full window (aside from the steps in the next section) or customizing the behavior of the SandDance widget at the moment.

That being said, both SandDance and Polyglot Notebooks are relatively young technologies and will likely evolve and grow over time.

## SandDance without Polyglot Notebooks

If SandDance is interesting to you and you don't want to use it inside of Polyglot Notebooks, there's a VS Code extension that allows you to run SandDance for any `.csv` file you have saved to disk.

First, [install the SandDance extension](https://marketplace.visualstudio.com/items?itemName=msrvida.vscode-sanddance) into VS Code.

Next, right click on any `.csv` file to show the `View in SandDance` context menu option.

![View in SandDance](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vhaehbgx0x111jhmh4df.png)

From there you'll be able to explore data in SandDance as if you had imported it with Polyglot Notebooks.

In fact, I often prefer this way of working with SandDance because it gives you more screen space to work with and SandDance as an extension respects your VS Code theme while the version that integrates with Polyglot Notebooks does not.

## Closing Thoughts

SandDance is an amazing experience, but I don't see it meeting all of my data visualization needs at the moment.

Specifically, I can see myself wanting to make small lightweight visualizations with a number of pre-configured settings and wanting those visualizations to be embedded in my notebook. This way I could make a scatter plot of a specific type and embed it in my dashboard and share it with other people for a more guided experience like I can in a Jupyter Notebook with visualization libraries like Plotly.

Still, SandDance is a great tool to have in your toolbox and I am hopeful about the direction this technology is growing.