From 1835a746cf9a197c4cf7a1c4bb419e2ca19da404 Mon Sep 17 00:00:00 2001 From: Markus Haller Date: Mon, 13 Nov 2023 15:58:21 +0100 Subject: [PATCH] cache single call of ``api.calculate`` #87 --- app/ptxboa_functions.py | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/app/ptxboa_functions.py b/app/ptxboa_functions.py index 5490e820..9551731c 100644 --- a/app/ptxboa_functions.py +++ b/app/ptxboa_functions.py @@ -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 @@ -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