Spaces:
Running
Running
File size: 3,711 Bytes
dfdb553 88ebfce dfdb553 88ebfce dfdb553 88ebfce dfdb553 88ebfce dfdb553 88ebfce dfdb553 88ebfce dfdb553 88ebfce dfdb553 88ebfce dfdb553 88ebfce dfdb553 88ebfce | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 | from dash import Dash, html, dcc, Input, Output, dash_table
import dash_bootstrap_components as dbc
import pandas as pd
import plotly.express as px
# -----------------------------
# Dados de exemplo
# -----------------------------
df = pd.DataFrame({
"Categoria": ["A", "B", "C", "D", "E"],
"Vendas": [120, 200, 150, 80, 220],
"Lucro": [30, 70, 40, 20, 90]
})
# -----------------------------
# Criar app
# -----------------------------
app = Dash(__name__, external_stylesheets=[dbc.themes.BOOTSTRAP])
server = app.server
# -----------------------------
# Layout
# -----------------------------
app.layout = dbc.Container([
dbc.Row([
dbc.Col([
html.H1("Dashboard em Dash", className="text-center my-4"),
html.P(
"Exemplo simples de dashboard com Dash + Bootstrap + Plotly.",
className="text-center text-muted"
)
])
]),
dbc.Row([
dbc.Col([
html.Label("Escolhe a métrica:", className="fw-bold"),
dcc.Dropdown(
id="coluna-dropdown",
options=[
{"label": "Vendas", "value": "Vendas"},
{"label": "Lucro", "value": "Lucro"}
],
value="Vendas",
clearable=False
)
], md=4)
], className="mb-4"),
dbc.Row([
dbc.Col(
dbc.Card(
dbc.CardBody([
html.H5("Total", className="card-title"),
html.H2(id="total-card", className="text-primary")
])
),
md=4
),
dbc.Col(
dbc.Card(
dbc.CardBody([
html.H5("Média", className="card-title"),
html.H2(id="media-card", className="text-success")
])
),
md=4
),
dbc.Col(
dbc.Card(
dbc.CardBody([
html.H5("Máximo", className="card-title"),
html.H2(id="max-card", className="text-danger")
])
),
md=4
),
], className="mb-4"),
dbc.Row([
dbc.Col([
dcc.Graph(id="grafico-barras")
], md=8),
dbc.Col([
dash_table.DataTable(
id="tabela",
columns=[{"name": i, "id": i} for i in df.columns],
data=df.to_dict("records"),
style_table={"overflowX": "auto"},
style_cell={
"textAlign": "center",
"padding": "10px"
},
style_header={
"backgroundColor": "#f8f9fa",
"fontWeight": "bold"
},
page_size=5
)
], md=4)
])
], fluid=True)
# -----------------------------
# Callback
# -----------------------------
@app.callback(
Output("grafico-barras", "figure"),
Output("total-card", "children"),
Output("media-card", "children"),
Output("max-card", "children"),
Input("coluna-dropdown", "value")
)
def atualizar_dashboard(coluna_escolhida):
fig = px.bar(
df,
x="Categoria",
y=coluna_escolhida,
color="Categoria",
title=f"{coluna_escolhida} por Categoria"
)
total = df[coluna_escolhida].sum()
media = round(df[coluna_escolhida].mean(), 2)
maximo = df[coluna_escolhida].max()
return fig, total, media, maximo
# -----------------------------
# Run app
# -----------------------------
if __name__ == "__main__":
app.run(host="0.0.0.0", port=7860, debug=False) |