Error showing a bar_chart_race video

Hi people
This is my first post since I started to develop my streamlit solution and til now I was able to solve the problems just searching in the forums or SO.

But I’m facing a problem that it’s kind weird.

I’m using bar_chart_race to create a video of a race of some data and trying to display it in a st.video component.

I followed the workaround given by snehankekre in this link How to plot race bar chart in streamlit - #6 by snehankekre

My code is as follows:

packages.txt

locales-all
ffmpeg

requirements.txt

pandas<=1.5.3
numpy<=1.25.2
plotly<=5.15.0
streamlit
git+https://github.com/dexplo/bar_chart_race.git@master

I’m not using the pip install bar_chart_race package because the version is outdated (it doesn’t have version 0.2.0)

My code is as follows:

import pandas as pd
import numpy as np
import locale
import plotly.express as px
import plotly.graph_objects as go
import streamlit as st
import streamlit.components.v1 as components
import io
import bar_chart_race as bcr
import base64

@st.cache_data
def plot_graph_race(df_data):
    df_values, df_ranks = bcr.prepare_long_data(df_data, 
                index='Year', 
                columns='Music', 
                values='Count', 
                steps_per_period=1)

    return bcr.bar_chart_race(df_values,
                n_bars=10, 
                steps_per_period=15, 
                period_length=1000, 
                title = 'Top 10 Music', 
                period_template='{x:.0f}', 
                fixed_max=True, 
                filter_column_colors=True).data


html_str = plot_graph_race(df)

start = html_str.find('base64,')+len('base64,')
end = html_str.find('">')

video = base64.b64decode(html_str[start:end])
st.video(video)

The error given was:
AttributeError: 'str' object has no attribute 'data'

And here is the stacktrace:

Traceback (most recent call last):

  File "/home/adminuser/venv/lib/python3.10/site-packages/streamlit/runtime/scriptrunner/script_runner.py", line 542, in _run_script
    exec(code, module.__dict__)
  File "/mount/src/myfirstapp-denis/app.py", line 486, in <module>
    html_str = plot_graph_race(df)
  File "/home/adminuser/venv/lib/python3.10/site-packages/streamlit/runtime/caching/cache_utils.py", line 210, in wrapper
    return cached_func(*args, **kwargs)
  File "/home/adminuser/venv/lib/python3.10/site-packages/streamlit/runtime/caching/cache_utils.py", line 239, in __call__
    return self._get_or_create_cached_value(args, kwargs)
  File "/home/adminuser/venv/lib/python3.10/site-packages/streamlit/runtime/caching/cache_utils.py", line 266, in _get_or_create_cached_value
    return self._handle_cache_miss(cache, value_key, func_args, func_kwargs)
  File "/home/adminuser/venv/lib/python3.10/site-packages/streamlit/runtime/caching/cache_utils.py", line 322, in _handle_cache_miss
    computed_value = self._info.func(*func_args, **func_kwargs)
  File "/mount/src/myfirstapp-denis/app.py", line 295, in plot_graph_race
    return bcr.bar_chart_race(df_values, n_bars=10, steps_per_period=15, period_length=1000, title = 'Top 10 Music', period_template='{x:.0f}', fixed_max=True, filter_column_colors=True).data
AttributeError: 'str' object has no attribute 'data'

I would like to say that this works fine in my environment and also in Google Collab, but not in Streamlit Cloud.
Also, I tested the “.data” return in Kaggle and it also works fine.

What am I missing here?

Thanks in advance!

Ok, just figured out what was missing.

As per its documentation, bar_char_race needs the ipython package in order to return a HTML element. In my environment I had already it installed because another app development, but streamlit cloud does not.

Just added the package to the requirements.txt and now it’s working.

Thanks anyway!

1 Like

Glad you found a solution @denisbruno! Also, thank you for coming back to post your solution for others to benefit from in future.
Happy Streamlit-ing! :balloon:

2 Likes

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.