Add_rows silently fails on compound altair chart

:rotating_light: Before clicking “Create Topic”, please make sure your post includes the following information (otherwise, the post will be locked). :rotating_light:

Hello everyone, hope you can shed some light on my problem:
I am trying to add rows on a compound altair chart. The compound chart is needed to enable select/deselect on individual line charts.

here is the code:

import altair as alt
import streamlit as st
import pandas as pd

make = pd.DataFrame({'name': ['Honda', 'Ford', 'Dodge']})
fuel = pd.DataFrame({
    'Honda': [9, 8, 8, 7, 7],
    'Ford': [5, 4, 3, 2, 1],
    'Dodge': [6, 5, 5, 3, 4]
}).reset_index().melt(id_vars=['index'], var_name='name', value_name='fuel')

selection = alt.selection_multi(fields=['name'])
color = alt.condition(selection, alt.Color('name:N'), alt.value('lightgray'))
make_selector = alt.Chart(make).mark_rect().encode(y='name', color=color).add_selection(selection)
fuel_chart = alt.Chart(fuel).mark_line().encode(x='index', y=alt.Y('fuel', scale=alt.Scale(domain=[0, 10])), color='name').transform_filter(selection)

chart=st.altair_chart(make_selector|fuel_chart,use_container_width=True)
chart.add_rows(pd.DataFrame({'index':[5]*3,'name':['Honda', 'Ford', 'Dodge'],'fuel':[8,2,5]}))

The new values(8,2,5) don’t appear in the graph. However, if i only add the fuel_chart without the make_selector and remove the transform_filter the add_rows works.

#This code works !!!
fuel_chart = alt.Chart(fuel).mark_line().encode(x='index', y=alt.Y('fuel', scale=alt.Scale(domain=[0, 10])), color='name')

chart=st.altair_chart(fuel_chart,use_container_width=True)
chart.add_rows(pd.DataFrame({'index':[5]*3,'name':['Honda', 'Ford', 'Dodge'],'fuel':[8,2,5]}))

for the failing code no error is displayed but an uncaught exception happens in the js code:

6988.2d1a14e2.chunk.js:2 Uncaught (in promise) Error: Unrecognized data set: source
at c (6988.2d1a14e2.chunk.js:2:357139)
at NO (6988.2d1a14e2.chunk.js:2:813666)
at AN.TO [as change] (6988.2d1a14e2.chunk.js:2:813793)
at AN.insert (6988.2d1a14e2.chunk.js:2:826822)
at v.updateData (43.d95709b8.chunk.js:1:5977)
at v.componentDidUpdate (43.d95709b8.chunk.js:1:5418)
at Ms (main.ca9076db.js:2:2308447)
at Cl (main.ca9076db.js:2:2325236)
at t.unstable_runWithPriority (main.ca9076db.js:2:3104636)
at Ho (main.ca9076db.js:2:2267054)

App is running locally with
python 3.11.7
streamlit 1.29.0
altair 5.2.0
pandas 2.1.4

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