diff --git a/maindash.py b/maindash.py index 85fa4cb..17f2cc8 100644 --- a/maindash.py +++ b/maindash.py @@ -35,82 +35,80 @@ # options for dropdown components with all the keys DROPDOWN_OPTIONS_ALL = [ - Output('c-picker-3d', 'options'), - Output('x-picker-2d-left', 'options'), - Output('y-picker-2d-left', 'options'), - Output('c-picker-2d-left', 'options'), - Output('x-picker-2d-right', 'options'), - Output('y-picker-2d-right', 'options'), - Output('c-picker-2d-right', 'options'), - Output('x-picker-histogram', 'options'), - Output('x-picker-heatmap', 'options'), - Output('y-picker-heatmap', 'options'), - Output('y-picker-violin', 'options'), + Output("c-picker-3d", "options"), + Output("x-picker-2d-left", "options"), + Output("y-picker-2d-left", "options"), + Output("c-picker-2d-left", "options"), + Output("x-picker-2d-right", "options"), + Output("y-picker-2d-right", "options"), + Output("c-picker-2d-right", "options"), + Output("x-picker-histogram", "options"), + Output("x-picker-heatmap", "options"), + Output("y-picker-heatmap", "options"), + Output("y-picker-violin", "options"), ] # values for dropdown components with all the keys DROPDOWN_VALUES_ALL = [ - Output('c-picker-3d', 'value'), - Output('x-picker-2d-left', 'value'), - Output('y-picker-2d-left', 'value'), - Output('c-picker-2d-left', 'value'), - Output('x-picker-2d-right', 'value'), - Output('y-picker-2d-right', 'value'), - Output('c-picker-2d-right', 'value'), - Output('x-picker-histogram', 'value'), - Output('x-picker-heatmap', 'value'), - Output('y-picker-heatmap', 'value'), - Output('y-picker-violin', 'value'), + Output("c-picker-3d", "value"), + Output("x-picker-2d-left", "value"), + Output("y-picker-2d-left", "value"), + Output("c-picker-2d-left", "value"), + Output("x-picker-2d-right", "value"), + Output("y-picker-2d-right", "value"), + Output("c-picker-2d-right", "value"), + Output("x-picker-histogram", "value"), + Output("x-picker-heatmap", "value"), + Output("y-picker-heatmap", "value"), + Output("y-picker-violin", "value"), ] DROPDOWN_VALUES_ALL_STATE = [ - State('c-picker-3d', 'value'), - State('x-picker-2d-left', 'value'), - State('y-picker-2d-left', 'value'), - State('c-picker-2d-left', 'value'), - State('x-picker-2d-right', 'value'), - State('y-picker-2d-right', 'value'), - State('c-picker-2d-right', 'value'), - State('x-picker-histogram', 'value'), - State('x-picker-heatmap', 'value'), - State('y-picker-heatmap', 'value'), - State('y-picker-violin', 'value'), + State("c-picker-3d", "value"), + State("x-picker-2d-left", "value"), + State("y-picker-2d-left", "value"), + State("c-picker-2d-left", "value"), + State("x-picker-2d-right", "value"), + State("y-picker-2d-right", "value"), + State("c-picker-2d-right", "value"), + State("x-picker-histogram", "value"), + State("x-picker-heatmap", "value"), + State("y-picker-heatmap", "value"), + State("y-picker-violin", "value"), ] # options for dropdown components with categorical keys DROPDOWN_OPTIONS_CAT = [ - Output('x-picker-violin', 'options'), + Output("x-picker-violin", "options"), ] # values for dropdown components with categorical keys DROPDOWN_VALUES_CAT = [ - Output('x-picker-violin', 'value'), + Output("x-picker-violin", "value"), ] # options for dropdown components with categorical keys and `None` # for color dropdown components DROPDOWN_OPTIONS_CAT_COLOR = [ - Output('c-picker-histogram', 'options'), - Output('c-picker-violin', 'options'), - Output('c-picker-parallel', 'options'), + Output("c-picker-histogram", "options"), + Output("c-picker-violin", "options"), + Output("c-picker-parallel", "options"), ] # values for dropdown components with categorical keys and `None` # for color dropdown components DROPDOWN_VALUES_CAT_COLOR = [ - Output('c-picker-histogram', 'value'), - Output('c-picker-violin', 'value'), - Output('c-picker-parallel', 'value'), + Output("c-picker-histogram", "value"), + Output("c-picker-violin", "value"), + Output("c-picker-parallel", "value"), ] -app = dash.Dash(__name__, - meta_tags=[{ - 'name': 'viewport', - 'content': 'width=device-width,initial-scale=1' - }] - ) +app = dash.Dash( + __name__, + meta_tags=[{"name": "viewport", "content": "width=device-width,initial-scale=1"}], +) """ Global Variables """ -REDIS_HASH_NAME = os.environ.get('DASH_APP_NAME', app.title) -SPECIAL_FOLDERS = ['images'] +REDIS_HASH_NAME = os.environ.get("DASH_APP_NAME", app.title) +SPECIAL_FOLDERS = ["images"] diff --git a/utils.py b/utils.py index 103a98f..52663c0 100644 --- a/utils.py +++ b/utils.py @@ -141,7 +141,8 @@ def load_image(img_path): :rtype: _type_ """ try: - encoding = base64.b64encode(open(img_path, "rb").read()) + with open(img_path, "rb") as img_file: + encoding = base64.b64encode(img_file.read()) img = "data:image/jpeg;base64," + encoding.decode() except FileNotFoundError: img = None @@ -269,13 +270,13 @@ def prepare_figure_kwargs( return fig_kwargs -def cache_set(data, id, key_major, key_minor=None): +def cache_set(data, id_str, key_major, key_minor=None): """ Set data to Redis :param dict/str/pandas.Dataframe data data to be stored in Redis - :param str id + :param str id_str unique id (session id) :param str key_major major key name @@ -283,20 +284,25 @@ def cache_set(data, id, key_major, key_minor=None): minor key name """ if key_minor is None: - key_str = key_major + id + key_str = key_major + id_str else: - key_str = key_major + id + key_minor + key_str = key_major + id_str + key_minor frame_cache.set(key_str, data, expire=EXPIRATION) -def redis_set(data, id, key_major, key_minor=None): +def cache_expire(): + """_summary_""" + frame_cache.expire() + + +def redis_set(data, id_str, key_major, key_minor=None): """ Set data to Redis :param dict/str/pandas.Dataframe data data to be stored in Redis - :param str id + :param str id_str unique id (session id) :param str key_major major key name @@ -304,18 +310,18 @@ def redis_set(data, id, key_major, key_minor=None): minor key name """ if key_minor is None: - key_str = key_major + id + key_str = key_major + id_str else: - key_str = key_major + id + key_minor + key_str = key_major + id_str + key_minor redis_instance.set(key_str, pickle.dumps(data), ex=EXPIRATION) -def cache_get(id, key_major, key_minor=None): +def cache_get(id_str, key_major, key_minor=None): """ Get data from Redis - :param str id + :param str id_str unique id (session id) :param str key_major major key name @@ -326,19 +332,19 @@ def cache_get(id, key_major, key_minor=None): :rtype: dict/str/pandas.Dataframe """ if key_minor is None: - key_str = key_major + id + key_str = key_major + id_str else: - key_str = key_major + id + key_minor + key_str = key_major + id_str + key_minor val = frame_cache.get(key_str, default=None, retry=True) return val -def redis_get(id, key_major, key_minor=None): +def redis_get(id_str, key_major, key_minor=None): """ Get data from Redis - :param str id + :param str id_str unique id (session id) :param str key_major major key name @@ -349,9 +355,9 @@ def redis_get(id, key_major, key_minor=None): :rtype: dict/str/pandas.Dataframe """ if key_minor is None: - key_str = key_major + id + key_str = key_major + id_str else: - key_str = key_major + id + key_minor + key_str = key_major + id_str + key_minor val = redis_instance.get(key_str) diff --git a/views/scatter_3d_view.py b/views/scatter_3d_view.py index d86c3be..4598a74 100644 --- a/views/scatter_3d_view.py +++ b/views/scatter_3d_view.py @@ -42,7 +42,7 @@ from maindash import app from utils import filter_all -from utils import cache_set, cache_get, CACHE_KEYS, KEY_TYPES +from utils import cache_set, cache_get, CACHE_KEYS, KEY_TYPES, cache_expire from utils import load_data from utils import load_data_list from utils import load_image @@ -877,6 +877,8 @@ def regenerate_figure_background_callback( cache_set(trigger_idx, session_id, CACHE_KEYS["task_id"]) set_progress([0, "Buffering ... (0 %)"]) + cache_expire() + if file not in file_list: file_list.append(file) @@ -968,11 +970,11 @@ def regenerate_figure_background_callback( print("task cancelled") return {"dummy": 0} + cache_set(slider_arg, session_id, CACHE_KEYS["figure_idx"]) cache_set(fig, session_id, CACHE_KEYS["figure"], str(slider_arg)) cache_set(hover_strings, session_id, CACHE_KEYS["hover"], str(slider_arg)) cache_set(ref_fig, session_id, CACHE_KEYS["figure_ref"], str(slider_arg)) cache_set(fig_layout, session_id, CACHE_KEYS["figure_layout"], str(slider_arg)) - cache_set(slider_arg, session_id, CACHE_KEYS["figure_idx"]) percent = slider_arg / len(frame_list) * 100 set_progress( diff --git a/viz/viz.py b/viz/viz.py index cbbc5ab..b7debb1 100644 --- a/viz/viz.py +++ b/viz/viz.py @@ -294,7 +294,8 @@ def get_animation_data( if img_list is not None: try: - encoded_image = base64.b64encode(open(img_list[idx], "rb").read()) + with open(img_list[idx], "rb") as img_file: + encoded_image = base64.b64encode(img_file.read()) img = "data:image/jpeg;base64,"+encoded_image.decode() except FileNotFoundError: img = None