Skip to content

Commit

Permalink
Merge pull request arnaudmiribel#130 from arnaudmiribel/update_streamlit
Browse files Browse the repository at this point in the history
Replace memo with cache_data when possible
  • Loading branch information
arnaudmiribel authored Mar 29, 2023
2 parents 67a7c2e + 5d74585 commit 1b268dc
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 25 deletions.
41 changes: 23 additions & 18 deletions src/streamlit_extras/altex/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,15 @@
import pandas as pd
import streamlit as st

try:
from streamlit import cache_data # streamlit >= 1.18.0
except ImportError:
from streamlit import experimental_memo as cache_data # streamlit >= 0.89

from .. import extra


@st.experimental_memo
@cache_data
def url_to_dataframe(url: str) -> pd.DataFrame:
"""Collects a CSV/JSON file from a URL and load it into a dataframe, with appropriate caching (memo)
Expand Down Expand Up @@ -255,7 +260,7 @@ def _partial(*args, **kwargs):
sparkarea_chart = _partial(area_chart, spark=True, __name__="sparkarea_chart")


@st.experimental_memo
@cache_data
def example_line():
stocks = get_stocks_data()

Expand All @@ -267,7 +272,7 @@ def example_line():
)


@st.experimental_memo
@cache_data
def example_multi_line():
stocks = get_stocks_data()
line_chart(
Expand All @@ -279,7 +284,7 @@ def example_multi_line():
)


@st.experimental_memo
@cache_data
def example_bar():
stocks = get_stocks_data()
bar_chart(
Expand All @@ -290,7 +295,7 @@ def example_bar():
)


@st.experimental_memo
@cache_data
def example_hist():
stocks = get_stocks_data()
hist_chart(
Expand All @@ -300,7 +305,7 @@ def example_hist():
)


@st.experimental_memo
@cache_data
def example_scatter_opacity():
weather = get_weather_data()
scatter_chart(
Expand All @@ -312,7 +317,7 @@ def example_scatter_opacity():
)


@st.experimental_memo
@cache_data
def example_bar_horizontal():
weather = get_weather_data()
bar_chart(
Expand All @@ -323,7 +328,7 @@ def example_bar_horizontal():
)


@st.experimental_memo
@cache_data
def example_bar_log():
weather = get_weather_data()
bar_chart(
Expand All @@ -338,7 +343,7 @@ def example_bar_log():
)


@st.experimental_memo
@cache_data
def example_bar_sorted():
weather = get_weather_data()
bar_chart(
Expand All @@ -349,7 +354,7 @@ def example_bar_sorted():
)


@st.experimental_memo
@cache_data
def example_scatter():
weather = get_weather_data()
scatter_chart(
Expand All @@ -360,7 +365,7 @@ def example_scatter():
)


@st.experimental_memo
@cache_data
def example_hist_time():
weather = get_weather_data()
hist_chart(
Expand All @@ -375,7 +380,7 @@ def example_hist_time():
)


@st.experimental_memo
@cache_data
def example_sparkline():
stocks = get_stocks_data()
sparkline_chart(
Expand All @@ -388,7 +393,7 @@ def example_sparkline():
)


@st.experimental_memo
@cache_data
def example_sparkbar():
stocks = get_stocks_data()
sparkbar_chart(
Expand All @@ -400,7 +405,7 @@ def example_sparkbar():
)


@st.experimental_memo
@cache_data
def example_sparkarea():
random_data = get_random_data()
df = pd.melt(
Expand All @@ -420,7 +425,7 @@ def example_sparkarea():
)


@st.experimental_memo
@cache_data
def example_bar_stacked():
barley = get_barley_data()
bar_chart(
Expand All @@ -432,7 +437,7 @@ def example_bar_stacked():
)


@st.experimental_memo
@cache_data
def example_bar_normalized():
barley = get_barley_data()
bar_chart(
Expand All @@ -444,7 +449,7 @@ def example_bar_normalized():
)


@st.experimental_memo
@cache_data
def example_bar_normalized_custom():
barley = get_barley_data()
bar_chart(
Expand All @@ -456,7 +461,7 @@ def example_bar_normalized_custom():
)


@st.experimental_memo
@cache_data
def example_bar_grouped():
barley = get_barley_data()
bar_chart(
Expand Down
9 changes: 7 additions & 2 deletions src/streamlit_extras/chart_annotations/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@
import pandas as pd
import streamlit as st

try:
from streamlit import cache_data # streamlit >= 1.18.0
except ImportError:
from streamlit import experimental_memo as cache_data # streamlit >= 0.89

from .. import extra

try:
Expand All @@ -16,7 +21,7 @@
st.altair_chart = partial(st.altair_chart, theme="streamlit")


@st.experimental_memo
@cache_data
def get_data() -> pd.DataFrame:
source = pd.read_csv(
"https://raw.githubusercontent.com/vega/vega-datasets/next/data/stocks.csv"
Expand All @@ -25,7 +30,7 @@ def get_data() -> pd.DataFrame:
return source


@st.experimental_memo(ttl=60 * 60 * 24)
@cache_data(ttl=60 * 60 * 24)
def get_chart(data: pd.DataFrame) -> alt.Chart:
hover = alt.selection_single(
fields=["date"],
Expand Down
9 changes: 7 additions & 2 deletions src/streamlit_extras/chart_container/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,20 @@
import pandas as pd
import streamlit as st

try:
from streamlit import cache_data # streamlit >= 1.18.0
except ImportError:
from streamlit import experimental_memo as cache_data # streamlit >= 0.89

from .. import extra


@st.experimental_memo
@cache_data
def _to_csv(data: pd.DataFrame):
return data.to_csv().encode("utf-8")


@st.experimental_memo
@cache_data
def _to_parquet(data: pd.DataFrame):
return data.to_parquet()

Expand Down
11 changes: 8 additions & 3 deletions src/streamlit_extras/image_in_tables/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,15 @@
import pandas as pd
import streamlit as st

try:
from streamlit import cache_data # streamlit >= 1.18.0
except ImportError:
from streamlit import experimental_memo as cache_data # streamlit >= 0.89

from .. import extra


@st.experimental_memo
@cache_data
def get_dataframe() -> pd.DataFrame:
df = pd.DataFrame(
[
Expand Down Expand Up @@ -39,12 +44,12 @@ def get_dataframe() -> pd.DataFrame:


@extra
@st.experimental_memo
@cache_data
def table_with_images(df: pd.DataFrame, url_columns: Iterable):

df_ = df.copy()

@st.experimental_memo
@cache_data
def _path_to_image_html(path):
return '<img src="' + path + '" width="60" >'

Expand Down

0 comments on commit 1b268dc

Please sign in to comment.