def test_new_card_with_cyc_buf(page: Page): page['card1'] = dict(data=data(fields=sample_fields, size=-3)) page.save() expect( page.load(), make_page(card1=make_card( data=make_cyc_buf(fields=sample_fields, data=[None] * 3, i=0))))
async def serve(q: Q): if not q.client.initialized: q.client.initialized = True q.page['pricing'] = ui.plot_card(box='1 1 4 5', title='Interval', data=data(fields='product price', rows=[ ['spam', 1.49], ['eggs', 2.49], ['ham', 1.99], ], pack=True), plot=ui.plot([ ui.mark(type='interval', x='=product', y='=price', y_min=0) ]), events=['select_marks']) q.page['details'] = ui.markdown_card( box='1 6 4 2', title='Selected Product', content='Nothing selected.', ) await q.page.save() else: await handle_on(q)
def get_objects(n_callcenters): ''' Returns a mapping from names to objects representing the cards used in the ui ''' n_categories = 3 objects = dict() objects['outcomes'] = ui.plot_card(box='1 1 12 5', title='Outcomes by Call Center', data=data('country callcenter count', n_callcenters * n_categories), plot=ui.plot([ ui.mark(type='interval', x='=callcenter', y='=count', color='=country', stack='auto', y_min=0) ])) for i in range(n_callcenters): col = (i % 12) + 1 row = (i // 12) * 2 + 6 objects[f'utilcc{i}'] = ui.tall_gauge_stat_card( box=f'{col} {row} 1 2', title=f'CC {i:02d} util.', value= '={{intl perc style="percent" minimum_fraction_digits=2 maximum_fraction_digits=2}}', aux_value='', plot_color='$green', progress=0, data=dict(perc=0)) return objects
async def small_stat_card(q): cat = 'dog' val = 1 pc = 0.1 c = q.page.add( f'example', ui.small_series_stat_card( box='1 1 1 1', title='Verticoin', value= '=${{intl qux minimum_fraction_digits=2 maximum_fraction_digits=2}}', data=dict(qux=val, quux=pc), plot_category='foo', plot_type='interval', plot_value='qux', plot_color='$red', plot_data=data('foo qux', -20), plot_zero_value=0, )) await q.page.save() while val < 1000: time.sleep(1) cat, val, pc = cat, val + np.random.randint(0, 10), np.random.rand() c.data.qux = val c.data.quux = pc c.plot_data[-1] = [cat, val] await q.page.save()
async def draw_weekly_sales_plot(q: Q, plot_data): v = q.page.add( 'content', ui.plot_card( box='4 2 9 9', title='Walmart Weekly Sales Forecast', data=data('Date Weekly_Sales data_type', 0), plot=ui.plot([ ui.mark( type='point', x='=Date', y='=Weekly_Sales', x_scale='time', y_min=0, x_title='Date', y_title='Weekly Sales (USD)', color='=data_type', color_range=' '.join([WaveColors.red, WaveColors.purple]), size=6, fill_opacity=0.75, shape='circle' ) ]) )) v.data = plot_data await q.page.save()
def __init__(self, cards_manager: CardsManager, buffer_rows=15, expected_value=None, update_period=3): super(GeneralCard, self).__init__(width=2, height=1) self.last_update = time.time() self.general_stats = 0.0 self.update_period = update_period self.expected_value = expected_value self.cards_manager = cards_manager self.card = cards_manager.page.add( f'general_stats', ui.wide_series_stat_card(box=self.gen_box(), title=f'General', value='={{iter_sec}}', aux_value='', data=dict(iter_sec=0.0), plot_data=data('tick usage', -buffer_rows), plot_category='tick', plot_value='usage', plot_zero_value=0, plot_color='$green', plot_curve='smooth')) self.card.data.iter_sec = "No data"
def render_positive_pdp_plot(q: Q, shap_rows: List, selected_row_index: Optional[int]): max_contrib_col = shap_rows[0][0] if selected_row_index is None else None is_cat, max_contrib_col, churn_rows = churn_predictor.get_positive_explanation( selected_row_index, max_contrib_col) plot = [ ui.mark(type='interval', x='=label', y='=size', x_title=max_contrib_col, color=q.client.secondary_color, fill_opacity=0.5), ui.mark(type='line' if is_cat else 'point', x='=label', y='=value', color=q.client.primary_color, shape='circle'), ] if selected_row_index is not None: plot.append( ui.mark(x=churn_predictor.get_python_type(df[max_contrib_col] [selected_row_index]))) q.page['top_positive_plot'] = ui.plot_card( box='middle', title='Feature Most Contributing to Churn', data=data(['label', 'value', 'size'], rows=churn_rows), plot=ui.plot(plot))
def __init__(self, thread_num, cards_manager, buffer_rows=15, expected_value=None, max_boxes_in_line=10, width=2, height=1): super(ThreadSeriesCard, self).__init__(thread_num, max_boxes_in_line, width, height, cards_manager) self.expected_value = expected_value self.card = cards_manager.page.add( f'thread_{thread_num}_series', ui.wide_series_stat_card(box=self.gen_box(), title=f'Thread #{thread_num}', value='={{iter_sec}}', aux_value='', data=dict(iter_sec=0.0), plot_data=data('tick usage', -buffer_rows), plot_category='tick', plot_value='usage', plot_zero_value=0, plot_color='$green', plot_curve='smooth')) self.card.data.iter_sec = "No data"
def get_sentence_similarity_visualization(self): """ Get similarity visualization at sentence level. Returns: ui.visualization: Similarity visualization plot """ rows = [] for sent_1 in self.doc_1.sents: for sent_2 in self.doc_2.sents: rows.append((str(sent_1), str(sent_2), round(float(sent_1.similarity(sent_2)), 2))) return ui.visualization( plot=ui.plot([ui.mark( type='point', x='=sent_1', y='=sent_2', size='=similarity', shape='circle', fill_color=self.color )]), data=data( fields=['sent_1', 'sent_2', 'similarity'], rows=rows, pack=True ) )
def test_new_card_with_map_buf(page: Page): page['card1'] = dict(data=data(fields=sample_fields)) page.save() expect( page.load(), make_page(card1=make_card( data=make_map_buf(fields=sample_fields, data={}))))
def test_load_card_with_fix_buf(page: Page): page['card1'] = dict(data=data(fields=sample_fields, rows=[[1, 2, 3]])) page.save() expect( page.load(), make_page(card1=make_card( data=make_fix_buf(fields=sample_fields, data=[[1, 2, 3]]))))
def test_new_card_with_map_buf(): page = site['/test'] page.drop() page['card1'] = dict(data=data(fields=sample_fields)) page.save() assert compare( page.load(), make_page(card1=make_card( data=make_map_buf(fields=sample_fields, data={}))))
def test_load_card_with_fix_buf(): page = site['/test'] page.drop() page['card1'] = dict(data=data(fields=sample_fields, rows=[[1, 2, 3]])) page.save() assert compare( page.load(), make_page(card1=make_card( data=make_fix_buf(fields=sample_fields, data=[[1, 2, 3]]))))
def test_new_card_with_cyc_buf(): page = site['/test'] page.drop() page['card1'] = dict(data=data(fields=sample_fields, size=-3)) page.save() assert compare( page.load(), make_page(card1=make_card( data=make_cyc_buf(fields=sample_fields, data=[None] * 3, i=0))))
def add_chart(box, title, plot_type='interval'): return ui.plot_card(box=box, title=title, data=data('xvalue yvalue'), plot=ui.plot([ ui.mark(type=plot_type, x='=xvalue', y='=yvalue', color='=yvalue') ]))
def test_fix_buf_init(page: Page): c = page.add('card1', dict(data=data(fields=sample_fields, size=3))) c.data = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] page.save() expect( page.load(), make_page(card1=make_card(data=make_fix_buf( fields=sample_fields, data=[[1, 2, 3], [4, 5, 6], [7, 8, 9]], ))))
def plotting_data(q: Q): n = 100 df = pd.DataFrame({ 'length': np.random.rand(n), 'width': np.random.rand(n), 'data_type': np.random.choice(a=['Train', 'Test'], size=n, p=[0.8, 0.2]) }) plot_marks = [ ui.mark(type='point', x='=length', x_title='Length (cm)', y='=width', y_title='Width (cm)', color='=data_type', shape='circle') ] q.page['scatter_plot_card'] = ui.plot_card( box='1 1 4 4', title='Scatter Plot from Dataframe', data=data( fields=df.columns.tolist(), rows=df.values.tolist(), pack=True # Not required ), plot=ui.plot(marks=plot_marks)) q.page['scatter_viz_form_card'] = ui.form_card( box='1 5 4 4', items=[ ui.visualization( plot=ui.plot(marks=plot_marks), data=data( fields=df.columns.tolist(), rows=df.values.tolist(), pack=True # required ), ) ])
def render_shap_plot(q: Q, shap_rows: List, selected_row_index: Optional[int]): q.page['shap_plot'] = ui.plot_card( box=ui.box('top-plot', height='700px'), title='Shap explanation' if selected_row_index else 'Global Shap', data=data(['label', 'value'], rows=shap_rows), plot=ui.plot([ ui.mark(type='interval', x='=value', x_title='SHAP value', y='=label', color=q.client.secondary_color) ]))
def test_fix_buf_init(): page = site['/test'] page.drop() c = page.add('card1', dict(data=data(fields=sample_fields, size=3))) c.data = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] page.save() assert compare( page.load(), make_page(card1=make_card(data=make_fix_buf( fields=sample_fields, data=[[1, 2, 3], [4, 5, 6], [7, 8, 9]], ))))
def test_cyc_buf_init(page: Page): c = page.add('card1', dict(data=data(fields=sample_fields, size=-3))) c.data = [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]] # insert 4 instead of 3; should circle back page.save() expect( page.load(), make_page(card1=make_card(data=make_cyc_buf( fields=sample_fields, data=[[10, 11, 12], [4, 5, 6], [7, 8, 9]], i=1, ))))
def test_map_buf_init(page: Page): c = page.add('card1', dict(data=data(fields=sample_fields))) c.data = dict(foo=[1, 2, 3], bar=[4, 5, 6], baz=[7, 8, 9]) page.save() expect( page.load(), make_page(card1=make_card(data=make_map_buf(fields=sample_fields, data=dict( foo=[1, 2, 3], bar=[4, 5, 6], baz=[7, 8, 9], )))))
def test_map_buf_write(): page = site['/test'] page.drop() c = page.add('card1', dict(data=data(fields=sample_fields))) c.data.foo = [1, 2, 3] c.data.bar = [4, 5, 6] c.data.baz = [7, 8, 9] page.save() assert compare( page.load(), make_page(card1=make_card(data=make_map_buf(fields=sample_fields, data=dict( foo=[1, 2, 3], bar=[4, 5, 6], baz=[7, 8, 9], ))))) c.data.baz[1] = 42 page.save() assert compare( page.load(), make_page(card1=make_card(data=make_map_buf(fields=sample_fields, data=dict( foo=[1, 2, 3], bar=[4, 5, 6], baz=[7, 42, 9], ))))) c.data.baz[1] = [41, 42, 43] page.save() assert compare( page.load(), make_page(card1=make_card( data=make_map_buf(fields=sample_fields, data=dict( foo=[1, 2, 3], bar=[4, 5, 6], baz=[7, [41, 42, 43], 9], ))))) c.data.baz[1][1] = 999 page.save() assert compare( page.load(), make_page(card1=make_card( data=make_map_buf(fields=sample_fields, data=dict( foo=[1, 2, 3], bar=[4, 5, 6], baz=[7, [41, 999, 43], 9], )))))
def missing_value_plots(q: Q): plot1 = ui.visualization( ui.plot(marks=[ ui.mark(type='point', x=f'=col1', y=f'=col1', color='red', shape='circle', size=10) ]), data(fields=['col1'], rows=[[1], [2], [3]], pack=True), height='20%', width='90%', ) # This works plot2 = ui.visualization( ui.plot(marks=[ ui.mark(type='point', x=f'=col1', y=f'=col1', color='red', shape='circle', size=10) ]), data(fields=['col1'], rows=[[None], [2], [3]], pack=True), height='20%', width='90%', ) # This fails # plot3 = ui.visualization( # ui.plot(marks=[ui.mark(type='point', x=f'=col1', y=f'=col1', color='red', shape='circle', size=10)]), # data(fields=['col1'], rows=[[np.nan], [2], [3]], pack=True), # height='20%', width='90%', # ) q.page['plots'] = ui.form_card(box='1 1 5 -1', items=[plot1, plot2])
def test_map_buf_init(): page = site['/test'] page.drop() c = page.add('card1', dict(data=data(fields=sample_fields))) c.data = dict(foo=[1, 2, 3], bar=[4, 5, 6], baz=[7, 8, 9]) page.save() assert compare( page.load(), make_page(card1=make_card(data=make_map_buf(fields=sample_fields, data=dict( foo=[1, 2, 3], bar=[4, 5, 6], baz=[7, 8, 9], )))))
def test_cyc_buf_init(): page = site['/test'] page.drop() c = page.add('card1', dict(data=data(fields=sample_fields, size=-3))) c.data = [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]] # insert 4 instead of 3; should circle back page.save() assert compare( page.load(), make_page(card1=make_card(data=make_cyc_buf( fields=sample_fields, data=[[10, 11, 12], [4, 5, 6], [7, 8, 9]], i=1, ))))
def __init__(self, thread_num, max_value, cards_manager: CardsManager, buffer_rows=15, expected_value=None, max_boxes_in_line=10): super(ThreadPairCard, self).__init__(thread_num, width=2, height=3, max_boxes_in_line=max_boxes_in_line, cards_manager=cards_manager) self.max_value = max_value self.expected_value = expected_value box_1 = self.gen_box() box_1_list = [int(i) for i in box_1.split()] box_2 = f'{box_1_list[0]} {box_1_list[1] + 1} {box_1_list[2]} {box_1_list[3]}' self.series_card = self.cards_manager.page.add( f'thread_{thread_num}_series_paired', ui.wide_series_stat_card(box=box_1, title=f'Thread #{thread_num}', value='={{iter_sec}}', aux_value='', data=dict(iter_sec=0.0), plot_data=data('tick usage', -buffer_rows), plot_category='tick', plot_value='usage', plot_zero_value=0, plot_color='$green', plot_curve='smooth')) self.series_card.data.iter_sec = "No data" self.bar_card = self.cards_manager.page.add( f'thread_{thread_num}_bar_paired', ui.wide_bar_stat_card( box=box_2, title=f'Thread #{thread_num}', value='={{value}}', aux_value='={{pc}}%', plot_color='$green', progress=0, data={ 'value': 0, 'pc': 0 }, ))
def test_map_buf_write(page: Page): c = page.add('card1', dict(data=data(fields=sample_fields))) c.data.foo = [1, 2, 3] c.data.bar = [4, 5, 6] c.data.baz = [7, 8, 9] page.save() expect( page.load(), make_page(card1=make_card(data=make_map_buf(fields=sample_fields, data=dict( foo=[1, 2, 3], bar=[4, 5, 6], baz=[7, 8, 9], ))))) c.data.baz[1] = 42 page.save() expect( page.load(), make_page(card1=make_card(data=make_map_buf(fields=sample_fields, data=dict( foo=[1, 2, 3], bar=[4, 5, 6], baz=[7, 42, 9], ))))) c.data.baz[1] = [41, 42, 43] page.save() expect( page.load(), make_page(card1=make_card( data=make_map_buf(fields=sample_fields, data=dict( foo=[1, 2, 3], bar=[4, 5, 6], baz=[7, [41, 42, 43], 9], ))))) c.data.baz[1][1] = 999 page.save() expect( page.load(), make_page(card1=make_card( data=make_map_buf(fields=sample_fields, data=dict( foo=[1, 2, 3], bar=[4, 5, 6], baz=[7, [41, 999, 43], 9], )))))
def make_ui_plot(file_path: str): """Creates a scatter plot from two random columns in the csv file""" df = pd.read_csv(file_path) col1 = df.columns.tolist()[np.random.randint(0, df.shape[1])] col2 = df.columns.tolist()[np.random.randint(0, df.shape[1])] df = df.where(pd.notnull(df), None) plot = ui.visualization( ui.plot(marks=[ui.mark(type='point', x=f'={col1}', y=f'={col2}', x_title=col1, y_title=col2)]), data(fields=df.columns.tolist(), rows=df.values.tolist(), pack=True), ) return plot
def add_interval_card(box, title, value, plot_type='interval'): return ui.plot_card(box=box, title=title, data=data('counts division', 0), plot=ui.plot([ ui.mark( type=plot_type, x='=division', y='=counts', y_min=0, ), ui.mark(x=value, label='', ref_stroke_color="#86003c", ref_stroke_size=2), ]))
def test_cyc_buf_write(): page = site['/test'] page.drop() c = page.add('card1', dict(data=data(fields=sample_fields, size=-3))) c.data[0] = [1, 2, 3] c.data[1] = [4, 5, 6] c.data[2] = [7, 8, 9] c.data[100] = [10, 11, 12] # keys don't matter c.data[101] = [13, 14, 15] # keys don't matter page.save() assert compare( page.load(), make_page(card1=make_card(data=make_cyc_buf( fields=sample_fields, data=[[10, 11, 12], [13, 14, 15], [7, 8, 9]], i=2, )))) c.data[2][1] = 42 page.save() assert compare( page.load(), make_page(card1=make_card(data=make_cyc_buf( fields=sample_fields, data=[[10, 11, 12], [13, 14, 15], [7, 42, 9]], i=2, )))) c.data[2][1] = [41, 42, 43] page.save() assert compare( page.load(), make_page(card1=make_card(data=make_cyc_buf( fields=sample_fields, data=[[10, 11, 12], [13, 14, 15], [7, [41, 42, 43], 9]], i=2, )))) c.data[2][1][1] = 999 page.save() assert compare( page.load(), make_page(card1=make_card(data=make_cyc_buf( fields=sample_fields, data=[[10, 11, 12], [13, 14, 15], [7, [41, 999, 43], 9]], i=2, ))))