def draw(self, chart='table', render='html', file_name=None, width=None, height=None, title=None, keep_ui_state=True, annotation_df=None, targets='open_long'): if targets == 'open_long': df = self.open_long_df.copy() elif targets == 'open_short': df = self.open_long_df.copy() df[targets] = targets df = df.reset_index() drawer = Drawer( NormalData(df=df, annotation_df=annotation_df, category_field=targets, index_field='timestamp', is_timeseries=True)) drawer.draw(chart=chart, render=render, file_name=file_name, width=width, height=height, title=title, keep_ui_state=keep_ui_state)
def draw(self, render='html', file_name=None, width=None, height=None, title=None, keep_ui_state=True, annotation_df=None, target_type: TargetType = TargetType.open_long): if target_type == TargetType.open_long: df = self.open_long_df.copy() elif target_type == TargetType.open_short: df = self.open_short_df.copy() df['target_type'] = target_type.value if df_is_not_null(df): drawer = Drawer( NormalData(df=df, annotation_df=annotation_df, index_field='timestamp', is_timeseries=True)) drawer.draw_table(render=render, file_name=file_name, width=width, height=height, title=title, keep_ui_state=keep_ui_state)
def update_table_and_graph(page_current, page_size, sort_by, filter, intent, chart, rows, columns): if chart: property_map = {} for row in rows: property_map[row['property']] = { 'y_axis': row['y_axis'], 'chart': row['chart'] } dff = current_df if filter: filtering_expressions = filter.split(' && ') for filter_part in filtering_expressions: col_name, operator, filter_value = split_filter_part(filter_part) if operator in ('eq', 'ne', 'lt', 'le', 'gt', 'ge'): # these operators match pandas series operator method names dff = dff.loc[getattr(dff[col_name], operator)(filter_value)] elif operator == 'contains': dff = dff.loc[dff[col_name].str.contains(filter_value)] elif operator == 'datestartswith': # this is a simplification of the front-end filtering logic, # only works with complete fields in standard format dff = dff.loc[dff[col_name].str.startswith(filter_value)] # if sort_by: # dff = dff.sort_values( # [col['entity_id'] for col in sort_by], # ascending=[ # col['direction'] == 'asc' # for col in sort_by # ], # inplace=False # ) if intent in (IntentType.compare_self.value, IntentType.compare_to_other.value): graph_data, graph_layout = Drawer(NormalData(dff)).draw_compare(chart=chart, property_map=property_map, render=None, keep_ui_state=False) else: graph_data, graph_layout = Drawer(NormalData(dff)).draw(chart=chart, property_map=property_map, render=None, keep_ui_state=False) table_data = dff.iloc[page_current * page_size: (page_current + 1) * page_size ].to_dict('records') return table_data, \ dcc.Graph( id='chart-content', figure={ 'data': graph_data, 'layout': graph_layout } ) raise dash.exceptions.PreventUpdate()
def update_data_table(n_clicks, properties, codes: str, start_date, end_date): if n_clicks and properties: props = [] for prop in properties: props.append(json.loads(prop)) readers = properties_to_readers(properties=props, codes=codes, start_date=start_date, end_date=end_date) if readers: data_df = readers[0].data_df for reader in readers[1:]: if df_is_not_null(reader.data_df): data_df = data_df.join(reader.data_df, how='outer') global current_df current_df = data_df normal_data = NormalData(data_df) data_table = Drawer(data=normal_data).draw_data_table( id='data-table-content') # generate col setting table properties = normal_data.data_df.columns.to_list() df = pd.DataFrame( OrderedDict([('property', properties), ('y_axis', ['y1'] * len(properties)), ('chart', ['line'] * len(properties))])) # generate intents intents = normal_data.get_intents() intent_options = [{ 'label': intent.value, 'value': intent.value } for intent in intents] intent_value = intents[0].value return data_table, df.to_dict( 'records'), normal_data.get_table_type( ), intent_options, intent_value else: return 'no data,please reselect!', None, '', [{ 'label': 'compare_self', 'value': 'compare_self' }], 'compare_self' raise dash.exceptions.PreventUpdate()
def data_drawer(self) -> Drawer: # FIXME"refresh normal_data? self.normal_data = NormalData(df=self.data_df, category_field=self.category_field, index_field=self.time_field, is_timeseries=True) return Drawer(data=self.normal_data)
def test_normal_sample(): for table_type in TableType: drawer = Drawer( data=NormalData(NormalData.sample(table_type=table_type))) drawer.draw_table()
def result_drawer(self) -> Drawer: return Drawer( NormalData(df=self.result_df, index_field=self.time_field, is_timeseries=True))
def depth_drawer(self) -> Drawer: drawer = Drawer( NormalData(df=self.depth_df, index_field=self.time_field, is_timeseries=True)) return drawer
def data_drawer(self) -> Drawer: return Drawer(data=self.normal_data)