Jak zobrazit png soubory a csv tabulek v pythonu (pomlčka) dashboard

0

Otázka

Jsem velmi nový plotly a dash, stejně jako uživatelské ROZHRANÍ. Jsem shromáždil data formuláře twitter pomocí tweepy a dokončili analýzy a generovat png také.

Snažím se postavit palubní desce. Mám seznam obrázků(png) a seznam souborů (csv). Chci mít tlačítko "grafy" a v rozbalovacím seznamu "data" (v podstatě csv souborů) na ovládacím panelu.

Když jsem klikněte na "grafy", chcete-li zobrazit všechny obrázky (kolem 3) a když jsem si vybrat jeden soubor, pak chci jen zobrazit top 100 řádků csv tabulky.

Může mi někdo navrhnout kostru pro to a proto jsem se může stavět na vrcholu. Šel jsem přes některé příklady pro tlačítko z https://dash.plotly.com/dash-html-components/button a další a snažil se níže.

import dash
import dash_html_components as html
import dash_core_components as dcc
import base64

app = dash.Dash()

list_of_images = [
    "C:\\Users\\results\\sentiment_plot.png",
    "C:\\Users\\positive_wc.png",
    "C:\\Users\\negative_wc.png",
    "C:\\Users\\top_hashtag.png"
]

list_of_data = [
    "C:\\Users\\sentiment_by_location.csv",
    "C:\\Users\\geo_location_frequency.csv",
    "C:\\Users\\total_hashtag_frequency.csv",
]

app.layout = html.Div(children=[
    html.H1(children='Twitter analytics Report'),
    html.H2(children='''Image charts'''),
    dcc.Dropdown(
        id='image-dropdown',
        options=[{'label': i, 'value': i} for i in list_of_images],
        # initially display the first entry in the list
        value = list_of_images[0],
    ),
    html.Img(id='image'),

    dcc.Dropdown(
        id='data-dropdown',
        options=[{'label': i, 'value': i} for i in list_of_data],
        # initially display the first entry in the list
        value = list_of_data[0]
    ),
    html.Img(id='data')

])

@app.callback(
    dash.dependencies.Output('image', 'src'),
    [dash.dependencies.Input('image-dropdown', 'value')])
def update_image_src(image_path):
    # print the image_path to confirm the selection is as expected
    print('current image_path = {}'.format(image_path))
    encoded_image = base64.b64encode(open(image_path, 'rb').read())
    return 'data:image/png;base64,{}'.format(encoded_image.decode())

@app.callback(
    dash.dependencies.Output('data', 'src'),
    [dash.dependencies.Input('data-dropdown', 'value')])
def update_data_src(data_path):
    # print the image_path to confirm the selection is as expected
    print('current data_path = {}'.format(data_path))
    encoded_image = base64.b64encode(open(data_path, 'rb').read())
    return 'data:image/png;base64,{}'.format(encoded_image.decode())

if __name__ == '__main__':
    app.run_server(debug=True)

Výše uvedený příklad funguje pro imgaes ale pořád musím najít způsob, aby se všechny obrázky zobrazí ve stejné velikosti. Csv jsem jen seznam drop dolů. Nenašel jsem způsob, jak ukázat csv dat v tabulkové formě.

Je to stále ohromující pro mě zkusit použít případ jako výše. Díky.

1

Nejlepší odpověď

0

Zkoušel jsem i z další příklad, jak je uvedeno níže. To také nikdy zobrazovat obrázky.

import dash
import dash_core_components as dcc
import dash_html_components as html
import base64

# Launch the application:
app = dash.Dash()

app.layout = html.Div([
    html.Button('Clear', id='btn-nclicks-1', n_clicks=0),
    html.Button('sentiment_plot', id='btn-nclicks-2', n_clicks=0),
    html.Button('top_hashtag', id='btn-nclicks-3', n_clicks=0),
    html.Button('positive_wc', id='btn-nclicks-4', n_clicks=0),
    html.Button('negative_wc', id='btn-nclicks-5', n_clicks=0),
    html.Div(id='container-button-timestamp')
])

@app.callback(
    dash.dependencies.Output('container-button-timestamp', 'children'),
    dash.dependencies.Input('btn-nclicks-1', 'n_clicks'),
    dash.dependencies.Input('btn-nclicks-2', 'n_clicks'),
    dash.dependencies.Input('btn-nclicks-3', 'n_clicks'),
    dash.dependencies.Input('btn-nclicks-4', 'n_clicks'),
    dash.dependencies.Input('btn-nclicks-5', 'n_clicks')
)
def displayClick(btn1, btn2, btn3, btn4, btn5):
    changed_id = [p['prop_id'] for p in dash.callback_context.triggered][0]
    if 'btndisplayClick-nclicks-1' in changed_id:
        ### HAVE TO CLEAR THE CANVAS
        msg = 'None was clicked'
    if 'btn-nclicks-2' in changed_id:
        #msg = 'sentiment_plot was most recently clicked'
        encoded_image = base64.b64encode(open("C:\\Users\\sentiment_plot.png", 'rb').read())
        msg = 'data:image/png;base64,{}'.format(encoded_image.decode())
    elif 'btn-nclicks-3' in changed_id:
        #msg = 'top_hashtag was most recently clicked'
        encoded_image = base64.b64encode(open("C:\\Users\\top_hashtag.png", 'rb').read())
        msg = 'data:image/png;base64,{}'.format(encoded_image.decode())
    elif 'btn-nclicks-4' in changed_id:
        #msg = 'positive_wc was most recently clicked'
        encoded_image = base64.b64encode(open("C:\\Users\\positive_wc.png", 'rb').read())
        msg = 'data:image/png;base64,{}'.format(encoded_image.decode())
    elif 'btn-nclicks-5' in changed_id:
        #msg = 'negative_wc was most recently clicked'
        encoded_image = base64.b64encode(open("C:\\Users\\negative_wc.png", 'rb').read())
        msg = 'data:image/png;base64,{}'.format(encoded_image.decode())
    else:
        msg = 'None of the buttons have been clicked yet'
    return html.Div(msg)

if __name__ == '__main__':
    app.run_server(debug=True)
2021-11-13 18:46:41

V jiných jazycích

Tato stránka je v jiných jazycích

Русский
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
中文
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................