Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update caching.md #687

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions content/library/advanced-features/caching.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

<br />
Expand Down