1

I am trying to display results of PM4PY algorithm on streamlit webApp. I was advised to display it as image (it doesn't need to be image if you have other recommendation), however I am facing AttributeError: 'NoneType' object has no attribute 'read' when doing so - the error is coming from 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

My code:

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’)

The error I am getting:

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())

1 Answer 1

2

Convert the net to png and show it by streamlit. There is also an option to save in memory (commented out).

Code

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')

Output

enter image description here

Sign up to request clarification or add additional context in comments.

1 Comment

thanks. The first one worked (saving as png) the memory solution didn't work, gave me the same error when rendering on the webApp

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.