Search code examples
pythonerror-handlingstreamlit

Streamlit - AssertionError: Cannot pair 2 captions with 1 images


I have the following code:

import pm4py
import pandas as pd
import streamlit as st
import graphviz

st.set_page_config(page_title="Process Mining dashboard", page_icon=":chart_with_downwards_trend:", layout="wide")

file_path = r'path/to/file.csv'
event_log = pd.read_csv(file_path, sep=';')

df = pm4py.format_dataframe(event_log, case_id = 'case_id', activity_key = 'activity', timestamp_key = 'timestamp', 
                                   timest_format = '%Y-%m-%d %H:%M:%S%z')

pm_image = st.container()

bpmn_model = pm4py.discover_bpmn_inductive(df)
bpmn = 'bpmn.png'
pm4py.save_vis_bpmn(bpmn_model, bpmn)

pn, im, fm = pm4py.discover_petri_net_inductive(df)
petri = 'petri.png'
pm4py.save_vis_petri_net(pn, im, fm, petri)

images = {
            'bpmn': bpmn,
            'petri': petri
}

img = st.sidebar.selectbox("Select the visualization of the process.", list(images.keys()))
                             

with pm_image:
     st.image(images[img], caption=list(images.keys()))

But when running this I get the following error:

AssertionError: Cannot pair 2 captions with 1 images.

What I would like to have is a dropdown menu which is linked to st.image and shows the image which is selected in the dropdown menu. The dropdown is correctly displayed but not the image I select (instead, the error is raised).

How should I change my code?


Solution

  • You are displaying only one image. You should therefore call st.image with a string and not a list for the caption parameter:

    st.image(images[img], caption=img)
    

    As seen in the Streamlit documentation of st.image:

    caption (str or list of str): Image caption. If displaying multiple images, caption should be a list of captions (one for each image).