From d6ba44f992acf9bb8cf1cb566a0d00039aee1260 Mon Sep 17 00:00:00 2001 From: SimLif <48397095+SimLif@users.noreply.github.com> Date: Thu, 1 Jun 2023 21:24:55 +0800 Subject: [PATCH] Update caching.md Add another example that does not require downloading data from the Internet. --- content/library/advanced-features/caching.md | 44 ++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/content/library/advanced-features/caching.md b/content/library/advanced-features/caching.md index 49c17529d..ce9f6e444 100644 --- a/content/library/advanced-features/caching.md +++ b/content/library/advanced-features/caching.md @@ -92,6 +92,50 @@ st.button("Rerun") Run the app again. You'll notice that the slow download only happens on the first run. Every subsequent rerun should be almost instant! 💨 +Here is another example that does not require downloading data from the Internet. Run this app and wait for the loading process to complete, then refresh the web page to experience the benefits of caching! 🥳 +```python +import streamlit as st +import numpy as np +import pandas as pd +from time import time, sleep + +st.title('st.cache') + +# Using cache +a0 = time() +st.subheader('Using st.cache') + +@st.cache_data() +def load_data_a(flag=1): + df = pd.DataFrame( + np.random.rand(2000000, 5), + columns=['a', 'b', 'c', 'd', 'e'] + ) + sleep(5) + return df + +st.write(load_data_a()) +a1 = time() +st.info(a1-a0) + + +# Not using cache +b0 = time() +st.subheader('Not using st.cache') + +def load_data_b(flag=1): + df = pd.DataFrame( + np.random.rand(2000000, 5), + columns=['a', 'b', 'c', 'd', 'e'] + ) + sleep(5) + return df + +st.write(load_data_b()) +b1 = time() +st.info(b1-b0) +``` + #### Behavior