Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
rookiepeng committed Nov 14, 2023
1 parent 2a73448 commit ab786c4
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 69 deletions.
96 changes: 47 additions & 49 deletions maindash.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
40 changes: 23 additions & 17 deletions utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -269,53 +270,58 @@ 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
:param str 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
:param str 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
Expand All @@ -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
Expand All @@ -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)

Expand Down
6 changes: 4 additions & 2 deletions views/scatter_3d_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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(
Expand Down
3 changes: 2 additions & 1 deletion viz/viz.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit ab786c4

Please sign in to comment.