Script runs very very slow in streamlit web but normal in pycharm

I have some a piece of code on matplotlib plot, but found it was rendered very very slow(hours) in streamlit web page, while it finished only in some seconds in local pycharm.
Any one know what’s the cause ??
Paste my relevant code as below:

@st.cache
def plot_learning_curve(model, X, y, cv, train_sizes):
    train_sizes, train_scores, test_scores = learning_curve(model, X=X, y=y, cv=cv, train_sizes=train_sizes)
    train_scores_mean = np.mean(train_scores, axis=1)
    train_scores_std = np.std(train_scores, axis=1)
    test_scores_mean = np.mean(test_scores, axis=1)
    test_scores_std = np.std(test_scores, axis=1)
    fig, ax = plt.subplots(figsize=(12, 10))
    ax.set_xlabel("Training examples", fontsize=15)
    ax.set_ylabel("Accuracy", fontsize=15)
    ax.set_title("Model learning curve", fontsize=20)
    ax.grid()
    ax.fill_between(train_sizes, train_scores_mean - train_scores_std,
                    train_scores_mean + train_scores_std, alpha=0.1,
                    color="r")
    ax.fill_between(train_sizes, test_scores_mean - test_scores_std,
                    test_scores_mean + test_scores_std, alpha=0.1,
                    color="g")
    ax.plot(train_sizes, train_scores_mean, 'o-', color="r",
            label="Training score")
    ax.plot(train_sizes, test_scores_mean, 'o-', color="g",
            label="Test score")
    ax.legend(loc="best", fontsize=14)
    return fig


fig = plot_learning_curve(clf, X, y, cv, train_sizes)
st.pyplot(fig)

Hi @skywateryang, can you please share the link to the GitHub repo to check what’s causing the time lag?

hi @sree369nidhi

Here is my repo link:


And here is the link for streamlit share:
https://share.streamlit.io/skywateryang/ml_playground/main/DecisionTree.py

thank you

1 Like

Hey @skywateryang, I guess the problem arises due to the decorator on top of the plot method. As soon as I removed it, it worked perfectly fine.

remove this : @st.cache
def plot_leanring_curve(model, X, y, cv, train_sizes):

Hope this helps.

1 Like

thanks @sree369nidhi,

I have tried this way, it works well.
But I am just curious about why cache function do not work well, that means every time we have to re-run the plot function without cache

I am having that same doubt, but I don’t know the exact inner working of the cache function, engineering team might be able to explain and provide a workaround.