破折号,合并两个仪表板,添加一个按钮

发布于 2025-01-21 07:06:57 字数 2899 浏览 4 评论 0原文

我是Dash的新手。我创建了两个仪表板,这些仪表板从两个不同的数据范围内读取。他们单独工作。我想将它们合并为单个,使用户有可能使用下拉菜单访问它们。 我还想添加一个按钮(单击时)返回功能。

这些是两个仪表板:

第一个:

import pandas as pd
df1 = pd.read_csv('/df1.csv')

# Import libraries
from dash import Dash, html, dcc, Input, Output
import pandas as pd
import plotly.express as px



# Create the Dash app
app = Dash()

# Set up the app layout
options_dropdown = dcc.Dropdown(options=df1['options'].unique(),
                            value='wordcount')

app.layout = html.Div(children=[
    html.H1(children='offensive/non offensive username activity dashboard'),
    options_dropdown,
    dcc.Graph(id='df1')
])



# Set up the callback function
@app.callback(
    Output(component_id='df1', component_property='figure'),
    Input(component_id=options_dropdown, component_property='value')
)
def update_graph(sel_option):
    filtered_options = df1[df1['options'] == sel_option]
    bar_fig = px.bar(filtered_options,
                       x= "user", y = "value",
                       color='user',
                           color_discrete_map={
                            'off': '#d62728',
                            'non_off': 'green'},
                        title=f' average {sel_option}',
                        width=500, height=500)
    return bar_fig

print(df1)
# Run local server
if __name__ == '__main__':
    app.run_server(debug=True)

第二个仪表板:

import pandas as pd
df2 = pd.read_csv('/df2.csv')

# Import libraries
from dash import Dash, html, dcc, Input, Output
import pandas as pd
import plotly.express as px



# Create the Dash app
app = Dash()

# Set up the app layout
options_dropdown = dcc.Dropdown(options=df2['options'].unique(),
                            value='wordcount')

app.layout = html.Div(children=[
    html.H1(children='offensive/non offensive username activity dashboard'),
    options_dropdown,
    dcc.Graph(id='df2')
])



# Set up the callback function
@app.callback(
    Output(component_id='df2', component_property='figure'),
    Input(component_id=options_dropdown, component_property='value')
)
def update_graph(sel_option):
    filtered_options = df2[df2['options'] == sel_option]
    line_fig = px.line(filtered_options,
                       x= "Week_Number", y = "value",
                       color='offensive',
                           color_discrete_map={
                            1: '#d62728',
                            0: 'green'},
                        title=f' average {sel_option}')
    return line_fig

print(df2)
# Run local server
if __name__ == '__main__':
    app.run_server(debug=True)

这是我要实现按下按钮的功能:

sentences = []
df3 = pd.read_csv('/df3.csv')
def cool_function():
    ext = rd.randint(0,len(df3.offensive))
    return rd.choice(sentences).format(df3.author[ext], "", df3.text[ext])

如何在一个仪表板中合并这三个元素?

I'm pretty new to Dash. I created two dashboards that read from two different dataframes. They work well individually. I would like to merge them in a single one, giving the possibility to the user to access them using a dropdown menu.
I would like to also add a button which (when clicked) returns a function.

Those are the two dashboards:

first one:

import pandas as pd
df1 = pd.read_csv('/df1.csv')

# Import libraries
from dash import Dash, html, dcc, Input, Output
import pandas as pd
import plotly.express as px



# Create the Dash app
app = Dash()

# Set up the app layout
options_dropdown = dcc.Dropdown(options=df1['options'].unique(),
                            value='wordcount')

app.layout = html.Div(children=[
    html.H1(children='offensive/non offensive username activity dashboard'),
    options_dropdown,
    dcc.Graph(id='df1')
])



# Set up the callback function
@app.callback(
    Output(component_id='df1', component_property='figure'),
    Input(component_id=options_dropdown, component_property='value')
)
def update_graph(sel_option):
    filtered_options = df1[df1['options'] == sel_option]
    bar_fig = px.bar(filtered_options,
                       x= "user", y = "value",
                       color='user',
                           color_discrete_map={
                            'off': '#d62728',
                            'non_off': 'green'},
                        title=f' average {sel_option}',
                        width=500, height=500)
    return bar_fig

print(df1)
# Run local server
if __name__ == '__main__':
    app.run_server(debug=True)

second one:

import pandas as pd
df2 = pd.read_csv('/df2.csv')

# Import libraries
from dash import Dash, html, dcc, Input, Output
import pandas as pd
import plotly.express as px



# Create the Dash app
app = Dash()

# Set up the app layout
options_dropdown = dcc.Dropdown(options=df2['options'].unique(),
                            value='wordcount')

app.layout = html.Div(children=[
    html.H1(children='offensive/non offensive username activity dashboard'),
    options_dropdown,
    dcc.Graph(id='df2')
])



# Set up the callback function
@app.callback(
    Output(component_id='df2', component_property='figure'),
    Input(component_id=options_dropdown, component_property='value')
)
def update_graph(sel_option):
    filtered_options = df2[df2['options'] == sel_option]
    line_fig = px.line(filtered_options,
                       x= "Week_Number", y = "value",
                       color='offensive',
                           color_discrete_map={
                            1: '#d62728',
                            0: 'green'},
                        title=f' average {sel_option}')
    return line_fig

print(df2)
# Run local server
if __name__ == '__main__':
    app.run_server(debug=True)

and this is the function I want to implement pressing a button:

sentences = []
df3 = pd.read_csv('/df3.csv')
def cool_function():
    ext = rd.randint(0,len(df3.offensive))
    return rd.choice(sentences).format(df3.author[ext], "", df3.text[ext])

How do I merge those three elements in a single dashboard?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文