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)
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.
Thanks for stopping by! We use cookies to help us understand how you interact with our website.
By clicking βAccept allβ, you consent to our use of cookies. For more information, please see our privacy policy.
Cookie settings
Strictly necessary cookies
These cookies are necessary for the website to function and cannot be switched off. They are usually only set in response to actions made by you which amount to a request for services, such as setting your privacy preferences, logging in or filling in forms.
Performance cookies
These cookies allow us to count visits and traffic sources so we can measure and improve the performance of our site. They help us understand how visitors move around the site and which pages are most frequently visited.
Functional cookies
These cookies are used to record your choices and settings, maintain your preferences over time and recognize you when you return to our website. These cookies help us to personalize our content for you and remember your preferences.
Targeting cookies
These cookies may be deployed to our site by our advertising partners to build a profile of your interest and provide you with content that is relevant to you, including showing you relevant ads on other websites.