Exemplo n.º 1
0
def add_remove_field_or_cell(add_button_clicks, _, ___, ____,
                             jsonified_selected_lib, rows_div_children):
    """Adds field pair to row"""
    clicks_num = uuid.uuid4().int
    clicks_num = int(str(clicks_num)[-10:-1])
    ctx = dash.callback_context
    if (add_button_clicks == 0 and (ctx.triggered[0]['prop_id'].split('.')[0]\
        !="dynamic-remove-field" and ctx.triggered[0]['prop_id'].split('.')[0]\
        !="dynamic-add-twin-line" and ctx.triggered[0]['prop_id'].split('.')[0]\
        !="dynamic-remove-twin-line")) or \
            (ctx.triggered and ctx.triggered[0]['prop_id'].split('.')[0]=="dynamic-add-field"):
        field_pair = visuals.generate_field_pair(
            clicks_num, json.loads(jsonified_selected_lib))
        if len(rows_div_children) < 1:
            row_level_buttons = visuals.generate_row_level_buttons(clicks_num)
            new_row = html.Div(children=[
                dbc.Row(children=[
                    dbc.Col(field_pair, xs=6, md=4, lg=3, xl=2),
                    dbc.Col(row_level_buttons,
                            xs=6,
                            md=4,
                            lg=3,
                            xl=2,
                            align='center')
                ],
                        style={
                            'background-color': '#AAB8C2',
                            'margin-top': '-12px',
                            'border-bottom': '2px solid #ffcccb'
                        }),
                html.Div(children=[],
                         id={
                             'type': 'md+graphs-rows',
                             'index': clicks_num
                         }),
                html.Div(id={
                    'type': 'intermediate-visual-state',
                    'index': clicks_num
                },
                         style={'display': 'none'}),
            ],
                               id={
                                   'type': 'row+visual-div',
                                   'index': clicks_num
                               })

            rows_div_children.append(new_row)
        else:
            temp = rows_div_children[-1]['props']['children'][0]['props'][
                'children'].pop()
            rows_div_children[-1]['props']['children'][0]['props']['children']\
                .extend((dbc.Col(field_pair, xs=6, md=4, lg=3, xl=2),temp))
    elif (ctx.triggered and ctx.triggered[0]['prop_id'].split('.')[0]=="dynamic-remove-field")\
         and len(rows_div_children) > 0 and len(rows_div_children[-1]['props']['children'][0]['props']['children'])>=1:

        try:
            #Need to refactor with dictionary query tool like JMESPath
            value = rows_div_children[-1]['props']['children'][0]['props'][
                'children'][-2]['props']['children']['props']['children'][
                    'props']['children'][1]['props']['value']
        except IndexError:
            value = None
        except KeyError:
            value = None
        if value is not None:
            rows_div_children[-1]['props']['children'][1]['props'][
                'children'] = []
        try:
            rows_div_children[-1]['props']['children'][0]['props'][
                'children'].pop(-2)
        except IndexError:
            pass
        return rows_div_children
    elif (ctx.triggered and ctx.triggered[0]['prop_id'].split('.')[0]
          == "dynamic-add-twin-line"):
        if len(rows_div_children) == 0:
            style = {
                'background-color': '#AAB8C2',
                'margin-top': '-12px',
                'border-bottom': '2px solid #ffcccb'
            }
        else:
            style = {
                'background-color': '#AAB8C2',
                'margin-top': '1px',
                'border-bottom': '2px solid #ffcccb'
            }
        field_pair = visuals.generate_field_pair(
            clicks_num, json.loads(jsonified_selected_lib))
        row_level_buttons = visuals.generate_row_level_buttons(clicks_num)
        new_row = html.Div(children=[
            dbc.Row(children=[
                dbc.Col(field_pair, xs=6, md=4, lg=3, xl=2),
                dbc.Col(row_level_buttons,
                        xs=6,
                        md=4,
                        lg=3,
                        xl=2,
                        align='center')
            ],
                    style=style),
            html.Div(children=[],
                     id={
                         'type': 'md+graphs-rows',
                         'index': clicks_num
                     }),
            html.Div(id={
                'type': 'intermediate-visual-state',
                'index': clicks_num
            },
                     style={'display': 'none'}),
        ],
                           id={
                               'type': 'row+visual-div',
                               'index': clicks_num
                           })
        rows_div_children.append(new_row)
        return rows_div_children
    elif (ctx.triggered and ctx.triggered[0]['prop_id'].split('.')[0]
          == "dynamic-remove-twin-line") and len(rows_div_children) > 0:
        rows_div_children.pop()
        return rows_div_children
    return rows_div_children
Exemplo n.º 2
0
def analyze_or_alter_row_cell(_, __, ___, ____, jsonified_selected_lib,
                              row_visual_pair, visual_save):
    num_clicks = uuid.uuid4().int
    num_clicks = int(str(num_clicks)[-11:-1])
    ctx = dash.callback_context
    try:
        triggered_id = ctx.triggered[0]['prop_id'].split('.')[0].split(
            ':')[2].strip('}"')
        triggered_idx = ctx.triggered[0]['prop_id'].split('.')[0].split(
            ':')[1].split(',')[0]
    except IndexError:
        triggered_id = ctx.triggered[0]['prop_id'].split('.')[0]
    if triggered_id == "row-add-field":
        field_pair = visuals.generate_field_pair(
            num_clicks, json.loads(jsonified_selected_lib))
        row_visual_pair[0]['props']['children'] = [
            *row_visual_pair[0]['props']['children'][:-1],
            dbc.Col(field_pair, xs=6, md=4, lg=3, xl=2),
            row_visual_pair[0]['props']['children'][-1]
        ]
    elif triggered_id == 'row-remove-field':
        if len(row_visual_pair[0]['props']['children']) > 1:
            try:
                value = row_visual_pair[0]['props']['children'][-2]['props'][
                    'children']['props']['children']['props']['children'][1][
                        'props']['value']
            except KeyError:
                value = 'none'
            if value != 'none':
                row_visual_pair[1]['props']['children'] = []
            row_visual_pair[0]['props']['children'].pop(-2)
        else:
            pass

    elif triggered_id == 'row-analyze-button':
        analyze_clicks = triggered_idx
        visual_output = analysis.analyze_inputs(
            row_visual_pair, json.loads(jsonified_selected_lib))
        if isinstance(visual_output, list):
            visual_output1 = visual_output[0]
            visual_output1 = dcc.Graph(figure=visual_output1)
            if visual_output[1] == 'speedup':
                table = visual_output[-1]
                gpu_server_specs = visual_output[2]
                plot = dbc.Col(visual_output1,
                               xs=12,
                               md=8,
                               className='card-size')
                table = dbc.Col(table, xs=12, md=8)
            else:
                gpu_server_specs = visual_output[1]
                plot = dbc.Col(visual_output1, xs=12, md=8)
                table = dbc.Col(xs=12, md=8)
            gpu_server_specs = [
                dcc.Markdown(dedent(gpu_server_spec))
                for gpu_server_spec in gpu_server_specs
            ]
            if len(gpu_server_specs) % 3 == 0:
                className = 'gpu-specs-full-row'
                width = 4
            else:
                className = 'gpu-specs-col'
                width = 8
                if len(gpu_server_specs) % 2 == 0:
                    width = 4
                    className = 'gpu-specs-col2'
            specs_out = dbc.Row(children=[
                dbc.Col(spec_info, width=width, className=className)
                for spec_info in gpu_server_specs
            ],
                                no_gutters=True,
                                justify='start',
                                className='analyze-output',
                                id={
                                    'type': 'spec-info',
                                    'index': analyze_clicks
                                })
            children = [plot, table]
            visual_id = {'type': 'output-visuals', 'index': analyze_clicks}
            if json.loads(jsonified_selected_lib).lower() == 'rocrand':
                visual_id = {
                    'type': 'output-visuals-rand',
                    'index': analyze_clicks
                }
                children = visuals.add_rand_slider(children, analyze_clicks)
                outcome = get_what_to_save(
                    row_visual_pair,
                    json.loads(jsonified_selected_lib).lower())
                visual_save = json.dumps(outcome)
            elif json.loads(jsonified_selected_lib).lower() == 'rocblas':
                children = visuals.add_blas_slider(children, analyze_clicks)
                visual_id = {
                    'type': 'output-visuals-blas',
                    'index': analyze_clicks
                }
                outcome = get_what_to_save(
                    row_visual_pair,
                    json.loads(jsonified_selected_lib).lower())
                visual_save = json.dumps(outcome)
            visuals_out = dbc.Row(children=children,
                                  justify='start',
                                  className='analyze-output plots',
                                  id=visual_id)
            try:
                row_visual_pair[1]['props']['children'] = [
                    specs_out, visuals_out
                ]
            except IndexError:
                pass
            except ValueError:
                pass
            except KeyError:
                pass
        else:
            error = dbc.Col(html.Div(visual_output,
                                     className='analyze-output'),
                            xs=3,
                            lg=2)
            row_visual_pair[1]['props']['children'] = [
                dbc.Row(children=[error], justify='end')
            ]
    elif triggered_id == 'row-toggle-button':
        row_visual_pair[1]['props']['children'] = []
    else:
        return [row_visual_pair, visual_save]

    return [row_visual_pair, visual_save]