Skip to content

Commit

Permalink
Merge pull request #88 from agoenergy/87-cache-results-from-apicalculate
Browse files Browse the repository at this point in the history
cache single call of ``api.calculate`` #87
  • Loading branch information
markushal authored Nov 13, 2023
2 parents 5a000b2 + 1835a74 commit 0a932c5
Showing 1 changed file with 19 additions and 4 deletions.
23 changes: 19 additions & 4 deletions app/ptxboa_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,24 @@
from ptxboa.api import PtxboaAPI


def calculate_results_single(api: PtxboaAPI, settings):
"""Calculate results for single country pair."""
res = api.calculate(**settings)
@st.cache_data()
def calculate_results_single(_api: PtxboaAPI, settings: dict) -> pd.DataFrame:
"""Calculate results for a single set of settings.
Parameters
----------
api : :class:`~ptxboa.api.PtxboaAPI`
an instance of the api class
settings : dict
settings from the streamlit app. An example can be obtained with the
return value from :func:`ptxboa_functions.create_sidebar`.
Returns
-------
pd.DataFrame
same format as for :meth:`~ptxboa.api.PtxboaAPI.calculate()`
"""
res = _api.calculate(**settings)

return res

Expand Down Expand Up @@ -46,7 +61,7 @@ def calculate_results(
for region in region_list:
settings2 = settings.copy()
settings2["region"] = region
res_single = api.calculate(**settings2)
res_single = calculate_results_single(api, settings2)
res_list.append(res_single)
res = pd.concat(res_list)
return res
Expand Down

0 comments on commit 0a932c5

Please sign in to comment.