在 Streamlit Web 应用程序上显示图像时出现 AttributeError

AttributeError when displaying image on streamlit web app

我正在尝试在 streamlit webApp 上显示 PM4PY 算法的结果。我被建议将它显示为图像(如果你有其他建议,它不需要是图像),但是我在这样做时面临 AttributeError: 'NoneType' object has no attribute 'read' - 错误来自 image.open()

case_id activity_id timestamp
1 Accepted 2021-12-20T15:52:47
1 Awaiting Documentation 2021-12-20T14:57:58
1 Complete Activated 2021-12-20T14:59:14
2 Approved 2021-12-20T14:57:59

我的代码:

import pm4py
import streamlit as st
from PIL import Image

@st.cache
def prepare_df():
    df1 = df[["case_id", "activity_id", "timestamp"]]
    df1["case_id"] = df1["case_id"].astype(str)
    df1["activity_id"] = df1["activity_id"].astype(str)
    df_log = pm4py.format_dataframe(df1, case_id='case_id', activity_key='activity_id', timestamp_key='timestamp')
    return df_log

df_log = prepare_df()

bpm_discovery = st.container()

map2 = pm4py.discover_heuristics_net(df_log)
image2 = Image.open(pm4py.view_heuristics_net(map2))

with bpm_discovery:
     st.image(image2, caption=‘Heuristic Minners algorithm’)

我得到的错误:

AttributeError: 'NoneType' object has no attribute 'read'
Traceback:
File "C:\Users\HAXY8W\PycharmProjects\processAnalysisApp\venv\lib\site-packages\streamlit\scriptrunner\script_runner.py", line 443, in _run_script
    exec(code, module.__dict__)
File "C:\Users\HAXY8W\PycharmProjects\processAnalysisApp\processAnalysis.py", line 83, in <module>
    image2 = Image.open(pm4py.view_heuristics_net(map2))
File "C:\Users\HAXY8W\PycharmProjects\processAnalysisApp\venv\lib\site-packages\PIL\Image.py", line 3074, in open
    fp = io.BytesIO(fp.read())

将网络转换为png并通过streamlit显示。还有一个保存在内存中的选项(注释掉)。

代码

import pm4py
import streamlit as st
# from PIL import Image
import pandas as pd
import io


d = {
    'case_id': [1, 1, 1, 2],
    'activity_id': ['Accepted', 'Awaiting Documentation',
    'Complete Activated', 'Approved'],
    'timestamp': ['2021-12-20T15:52:47', '2021-12-20T14:57:58',
    '2021-12-20T14:59:14', '2021-12-20T14:57:59']
}

@st.cache
def prepare_df(df):
    df1 = df[["case_id", "activity_id", "timestamp"]]
    df1["case_id"] = df1["case_id"].astype(str)
    df1["activity_id"] = df1["activity_id"].astype(str)
    df_log = pm4py.format_dataframe(df1, case_id='case_id', activity_key='activity_id', timestamp_key='timestamp')
    return df_log

st.title('pm4py on streamlit')

df = pd.DataFrame(d)

df_log = prepare_df(df)

bpm_discovery = st.container()

map2 = pm4py.discover_heuristics_net(df_log)

# 1. Save to file and show it by streamlit.
fn = 'a.png'
pm4py.save_vis_heuristics_net(map2, fn)

# 2. Save to memory.
# image2 = io.BytesIO(pm4py.view_heuristics_net(map2))

with bpm_discovery:
     st.image(fn, caption='Heuristic Minners algorithm')

输出