Problem to show visualization (arulesviz)

Someone can help me to show the interactive chart from GitHub - lispsil/python-arulesviz: Arulesviz - interactive association rules vizualization tool for python? I try on notebook and google collab using this code and success.

g = Arulesviz(no, 0.02, 0.7, 0.5, products_to_drop=)
g.create_rules()
g.plot_graph(width=1800, directed=False, charge=-150, link_distance=20)

but when I try it on streamlit using st.write, it shows like this

When I try the same code as in the notebook, it shows nothing on streamlite.

arulesviz

The arulesviz library returns an ipywidgets object, which are made for visualize stuff in Jupyter Notebooks; streamlit does not support ipywidgets right out the box.

An alternative is to export the ipywidget object (i.e., the arulesviz figure) into a standalone HTML that can then be added to streamlit as a new component.

Here is the code to get the example from above:
import streamlit as st
from io import StringIO
from ipywidgets.embed import embed_minimal_html
from arulesviz import Arulesviz

"# 🖇 `arulesviz`"

WIDTH = 700

#- Example taken from `https://github.com/lispsil/python-arulesviz/blob/master/examples/groceries.ipynb`" 
transactions = []
to_drop = set()

with open('groceries.csv') as f:
    for row in f:
        q = set(row.strip().split(',')) - to_drop
        if len(q) > 1: transactions.append(q)

g = Arulesviz(
    transactions, 0.001, 0.3, 12, 
    products_to_drop=['other vegetables', 'whole milk', 'yogurt', 'root vegetables','fruit/vegetable juice']
    )

g.create_rules()

fig = g.plot_graph(width=WIDTH, directed=False, charge=-200, link_distance=30)

## Exports the ipywidget to HTML and embeds it to streamlit
with StringIO() as f:
    embed_minimal_html(f, [fig], title="Hey!")
    fig_html = f.getvalue()

st.components.v1.html(fig_html, width=WIDTH, height=700, scrolling=True)

:warning: you will need a version of ipywidgets below 7.7 because of something on the embed_minimal_html function of ipywidgets 8 that does not play along with streamlit.

3 Likes

thank you! finally I get the answer :+1: