def _cache_get_user(self, user_id): """ Gets a Friend Connect user from the cache. Args: user_id: The ID of the user to fetch from the cache. Returns: The user with the specified ID if they exist in the cache, None otherwise. """ utils.cache_get("fcuser", user_id)
def _yql_query(self, query): """ Performs a query on the YQL web service interface. Args: query: A string query in the YQL query syntax. Returns: If a result was returned, a dict representing the data structure which was passed back. None otherwise. """ query_hash = hashlib.sha1(query).hexdigest() result = utils.cache_get("yql_query", query_hash) if result is None: logging.info("Fetching yql query: %s" % query) query = urllib.quote_plus(query) url = "http://query.yahooapis.com/v1/public/yql?q=%s&format=json" % query response = simplejson.loads(urlfetch.fetch(url).content) if response is None: return None response = response["query"] if response is None or int(response["count"]) == 0: return None # Result set is inconsistent if there is only one result if int(response["count"]) == 1: result = [response["results"]["Result"]] else: result = response["results"]["Result"] utils.cache_set(result, "yql_query", query_hash) return result
def __new__(cls, key=None, auto_create=False): """ Gets a session with the corresponding key. Args: key: The key corresponding to the session to retrieve data for. auto_create: If this is set to True and no session is found with the corresponding key, one will be created - used for creating sessions for Friend Connect users (since the key is generated by Friend Connect, not by this provider). Returns: A SessionProvider or None if no session existed for the given key and auto_create was False. """ session = None if key is None: key = SessionProvider.generate_session_key() else: data = utils.cache_get("session", key) if data is not None: session = pickle.loads(data) if not session: session = dict.__new__(cls, {}) session.__key = key session["started"] = time.time() return session
def _yql_query(self, query): """ Performs a query on the YQL web service interface. Args: query: A string query in the YQL query syntax. Returns: If a result was returned, a dict representing the data structure which was passed back. None otherwise. """ query_hash = hashlib.sha1(query).hexdigest() result = utils.cache_get("yql_query", query_hash) if result is None: logging.info("Fetching yql query: %s" % query) query = urllib.quote_plus(query) url = "http://query.yahooapis.com/v1/public/yql?q=%s&format=json" % query response = simplejson.loads(urlfetch.fetch(url).content)["query"] if response is None or int(response["count"]) == 0: return None # Result set is inconsistent if there is only one result if int(response["count"]) == 1: result = [response["results"]["Result"]] else: result = response["results"]["Result"] utils.cache_set(result, "yql_query", query_hash) return result
def session_exists(session_key): """ Checks to see whether a session with the given key exists. Args: session_key: The string to check. Returns: True if a session exists with the given key, False otherwise. """ return utils.cache_get("session", session_key) is not None
def _cache_get_restaurant(self, restaurant_id): """ Gets a restaurant from the cache. Args: restaurant_id: The id of the restaurant to retrieve from the cache. Returns: The data for the restaurant with the corresponding ID, if it existed in the cache, else None. """ return utils.cache_get("restaurant", restaurant_id)
def left_hide_button(btn, trigger_idx, session_id): """ Callback when hide/unhide button is clicked :param int btn number of clicks :param int trigger_idx trigger value :param int session_id session id :return: trigger signal :rtype: int """ if btn == 0: raise PreventUpdate selectedData = cache_get(session_id, CACHE_KEYS['selected_data']) if selectedData is None: raise PreventUpdate visible_table = cache_get(session_id, CACHE_KEYS['visible_table']) s_data = pd.DataFrame(selectedData['points']) idx = s_data['id'] idx.index = idx vis_idx = idx[visible_table['_VIS_'][idx] == 'visible'] hid_idx = idx[visible_table['_VIS_'][idx] == 'hidden'] visible_table.loc[vis_idx, '_VIS_'] = 'hidden' visible_table.loc[hid_idx, '_VIS_'] = 'visible' cache_set(visible_table, session_id, CACHE_KEYS['visible_table']) return dict(output_trigger=trigger_idx + 1)
def update_buffer_indicator(interval, filter_trigger, colormap, left_hide_trigger, outline_sw, file_picker, max_frame, session_id): """ Update buffer progress bar :param int unused1 :param int unused2 :param str unused3 :param int unused4 :param boolean unused5 :param json unused6 :param int max_frame maximal number of frames :param str session_id session id :return: [ Buffer percentage, Interval enable/disable ] :rtype: int """ if max_frame is None: return dict(buffer_progress=0, buffer_intrval_disabled=False) ctx = dash.callback_context trigger_id = ctx.triggered[0]['prop_id'].split('.')[0] if trigger_id == 'buffer-interval': fig_idx = cache_get(session_id, CACHE_KEYS['figure_idx']) if fig_idx is not None: percent = fig_idx / max_frame * 100 if percent == 100: return dict(buffer_progress=percent, buffer_intrval_disabled=True) else: return dict(buffer_progress=percent, buffer_intrval_disabled=dash.no_update) else: return dict(buffer_progress=0, buffer_intrval_disabled=dash.no_update) else: return dict(buffer_progress=0, buffer_intrval_disabled=False)
:param list visible_list visibility list :param str case case name :param json file selected file :return: [ Violin graph, X axis picker enable/disable, Y axis picker enable/disable, Color picker enable/disable, ] :rtype: list """ config = cache_get(session_id, CACHE_KEYS['config']) filter_kwargs = cache_get(session_id, CACHE_KEYS['filter_kwargs']) cat_keys = filter_kwargs['cat_keys'] num_keys = filter_kwargs['num_keys'] cat_values = filter_kwargs['cat_values'] num_values = filter_kwargs['num_values'] x_key = x_violin if x_violin is None: raise PreventUpdate x_label = config['keys'][x_violin].get('description', x_key) y_key = y_violin y_label = config['keys'][y_violin].get('description', y_key)
dim_picker_val=Output('dim-picker-parallel', 'value'), dp_opts_all=DROPDOWN_OPTIONS_ALL, dp_vals_all=DROPDOWN_VALUES_ALL, dp_opts_cat_color=DROPDOWN_OPTIONS_CAT_COLOR, dp_vals_cat_color=DROPDOWN_VALUES_CAT_COLOR, dp_opts_cat=DROPDOWN_OPTIONS_CAT, dp_vals_cat=DROPDOWN_VALUES_CAT), inputs=dict(file=Input('file-picker', 'value')), state=dict(file_loaded=State('file-loaded-trigger', 'data'), case=State('case-picker', 'value'), session_id=State('session-id', 'data'), all_state=DROPDOWN_VALUES_ALL_STATE)) def file_select_changed(file, file_loaded, case, session_id, all_state): # get keys from Redis config = cache_get(session_id, CACHE_KEYS['config']) # extract keys and save to Redis num_keys = [] cat_keys = [] for _, item in enumerate(config['keys']): if config['keys'][item].get('type', KEY_TYPES['NUM']) == KEY_TYPES['NUM']: num_keys.append(item) else: cat_keys.append(item) filter_kwargs = {'num_keys': num_keys, 'cat_keys': cat_keys} cache_set(filter_kwargs, session_id, CACHE_KEYS['filter_kwargs']) # options for `DROPDOWN_OPTIONS_ALL` options_all = [[{
ctx = dash.callback_context trigger_id = ctx.triggered[0]['prop_id'].split('.')[0] file = json.loads(file) # no update if: # - triggered from 3D scatter, and # - click_hide switch is disabled or the reference point is clicked if trigger_id == 'scatter3d' and \ ((not click_hide) or (click_data['points'][0]['curveNumber'] == 0)): raise PreventUpdate opacity = np.linspace(1, 0.2, decay + 1) # if slider value changed # - if Redis `figure` buffer ready, return figure from Redis if trigger_id == 'slider-frame': fig_idx = cache_get(session_id, CACHE_KEYS['figure_idx']) if fig_idx is not None: if slider_arg <= fig_idx: fig = cache_get(session_id, CACHE_KEYS['figure'], str(slider_arg)) if decay > 0: for val in range(1, decay + 1): if (slider_arg - val) >= 0: new_fig = cache_get(session_id, CACHE_KEYS['figure'], str(slider_arg - val)) new_fig[0]['marker']['opacity'] = opacity[val] fig = fig + new_fig fig_ref = cache_get(session_id, CACHE_KEYS['figure_ref'], str(slider_arg))
visibility list ['visible', 'hidden'] :param dict kwargs 'linewidth' outline width 'c_key' color key 'colormap' colormap name """ # set figure index to -1 (no buffer is ready) cache_set(-1, session_id, CACHE_KEYS['figure_idx']) # set new task_id in Redis, this will terminate the previously running task task_id = self.request.id cache_set(task_id, session_id, CACHE_KEYS['task_id']) config = cache_get(session_id, CACHE_KEYS['config']) keys_dict = config['keys'] slider_label = keys_dict[config['slider']]['description'] filter_kwargs = cache_get(session_id, CACHE_KEYS["filter_kwargs"]) cat_keys = filter_kwargs['cat_keys'] num_keys = filter_kwargs['num_keys'] num_values = filter_kwargs['num_values'] cat_values = filter_kwargs['cat_values'] visible_table = cache_get(session_id, CACHE_KEYS['visible_table']) frame_list = cache_get(session_id, CACHE_KEYS['frame_list']) dataset = pd.read_feather('./data/' + case + file['path'] + '/' + file['feather_name'])