def indenticon(text=None): if not text: return send_file("static/img/placeholder.jpg") try: size = int(request.args.get("size", 100)) except ValueError: abort(400) img_format = request.args.get("format", "jpeg").lower() if img_format not in SUPPORTED_FORMATS: abort(400) key = "-".join([text, str(size), img_format]) cached_icon = utils.cache_check(key) if cached_icon: data = cached_icon else: code = int(hexlify(text), 16) stream = utils.create_identicon(code, size, img_format) data = stream.getvalue() stream.close() utils.cache_set(key, data) response = Response(data, mimetype="image/" + img_format) return response
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 _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 _cache_restaurant(self, restaurant): """ Stores a restaurant in the memory cache. Args: restaurant: The object to store in the cache. The restaurant id is used as a cache key. """ utils.cache_set(restaurant, "restaurant", restaurant.restaurant_id)
def _cache_user(self, user): """ Stores a Friend Connect user in the cache. Args: user: User to store in the cache. The user's provider id is used as the cache key. """ utils.cache_set(user, "fcuser", user.provider_id)
def _write_session(self): """ Writes a serialized version of this object to the memory cache. This method writes the serialized version of this class to the memory cache for settings.SESSION_TIMEOUT seconds from now, meaning that all session data is cleared that many seconds from its last write. If the session has been marked as being "killed", no data will be written. """ if not self.__killed: lifespan = settings.SESSION_TIMEOUT utils.cache_set(pickle.dumps(self), "session", self.key, time=lifespan)
def select_left_figure(selectedData, session_id): """ Callback when data selected on the left 2D scatter :param json selectedData selected data :param str session_id session id :return: selected data :rtype: json """ cache_set(selectedData, session_id, CACHE_KEYS['selected_data']) return dict(dummy=0)
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)
Values for dropdown components with categorical keys and `None`, Options for dropdown components with categorical keys, Values for dropdown components with categorical keys ] :rtype: list """ if case is None: raise PreventUpdate data_files = [] case_dir = './data/' + case if os.path.exists('./data/' + case + '/config.json'): config = load_config('./data/' + case + '/config.json') cache_set(config, session_id, CACHE_KEYS['config']) else: raise PreventUpdate for dirpath, dirnames, files in os.walk(case_dir): dirnames[:] = [d for d in dirnames if d not in SPECIAL_FOLDERS] for name in files: if name.lower().endswith('.csv'): data_files.append({ 'label': os.path.join(dirpath[len(case_dir):], name), 'value': json.dumps({ 'path': dirpath[len(case_dir):], 'name': name, 'feather_name': name.replace('.csv', '.feather')
layout = cache_get(session_id, CACHE_KEYS['figure_layout'], str(slider_arg)) return dict(scatter3d=dict(data=fig_ref + fig, layout=layout), filter_trigger=dash.no_update) # get config from Redis config = cache_get(session_id, CACHE_KEYS['config']) keys_dict = config['keys'] # save filter key word arguments to Redis filter_kwargs = cache_get(session_id, CACHE_KEYS['filter_kwargs']) cat_keys = filter_kwargs['cat_keys'] num_keys = filter_kwargs['num_keys'] filter_kwargs['num_values'] = num_values filter_kwargs['cat_values'] = cat_values cache_set(filter_kwargs, session_id, CACHE_KEYS['filter_kwargs']) # get visibility table from Redis visible_table = cache_get(session_id, CACHE_KEYS['visible_table']) # get frame list from Redis frame_list = cache_get(session_id, CACHE_KEYS['frame_list']) # update visibility table if a data point is clicked to hide if trigger_id == 'scatter3d' and \ click_hide and \ click_data['points'][0]['curveNumber'] > 0: if visible_table['_VIS_'][click_data['points'][0]['id']] == 'visible': visible_table.at[click_data['points'][0]['id'], '_VIS_'] = 'hidden' else:
session id :param str case case name :param json file selected file :param list visible_picker 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']