Skip to content

Instantly share code, notes, and snippets.

@CoutinhoElias
Last active August 27, 2022 19:46
Show Gist options
  • Save CoutinhoElias/a22c18e3bbc993dc590cf1580731c798 to your computer and use it in GitHub Desktop.
Save CoutinhoElias/a22c18e3bbc993dc590cf1580731c798 to your computer and use it in GitHub Desktop.
meu problema
from dash import Dash
from dash_html_components import Div, H1, P, H3
from dash_core_components import Graph, Dropdown, Slider, Checklist
from dash.dependencies import Input, Output
from random import randint
import pandas as pd
app = Dash(__name__)
# my_json só está neste formato porque os dados vem de tabelas do Postgres e não tenho como envia-las a você
# por isso a transformei em dict
my_json = {
"vendedor": {
"0": "CARLA",
"1": "CARLA",
"2": "CARLA",
"3": "EDSON MARTINS",
"4": "EDSON MARTINS",
"5": "EDSON MARTINS",
"6": "EDSON MARTINS",
"7": "EDSON MARTINS",
"8": "EDSON MARTINS",
"9": "EDSON MARTINS",
"10": "EDSON MARTINS",
"11": "EDSON MARTINS",
"12": "EDSON MARTINS",
"13": "EDSON MARTINS",
"14": "EDSON MARTINS",
"15": "ISABEL",
"16": "ISABEL",
"17": "ISABEL",
"18": "ISABEL",
"19": "ISABEL",
"20": "ISABEL",
"21": "JULIANA CARDOSO",
"22": "JULIANA CARDOSO",
"23": "JULIANA CARDOSO",
"24": "JULIANA CARDOSO",
"25": "JULIANA CARDOSO",
"26": "JULIANA CARDOSO",
"27": "JULIANA CARDOSO",
"28": "JULIANA CARDOSO",
"29": "JULIANA CARDOSO",
"30": "JULIANA CARDOSO",
"31": "JULIANA CARDOSO",
"32": "JULIANA CARDOSO",
"33": "LORENA",
"34": "LORENA",
"35": "LORENA",
"36": "LORENA",
"37": "LORENA",
"38": "MARA",
"39": "MARA",
"40": "MARA",
"41": "MARA",
"42": "MARA",
"43": "MARA",
"44": "MARA",
"45": "MARA",
"46": "MARA",
"47": "MARA",
"48": "MAYARA",
"49": "MAYARA",
"50": "MAYARA",
"51": "MAYARA",
"52": "MAYARA",
"53": "MAYARA",
"54": "MAYARA",
"55": "MAYARA",
"56": "MAYARA",
"57": "MAYARA",
"58": "MAYARA",
"59": "MAYARA",
"60": "SUPERVISOR",
"61": "SUPERVISOR",
"62": "SUPERVISOR",
"63": "VANDERSON",
"64": "VANDERSON",
"65": "VANDERSON"
},
"MesAno": {
"0": "2020-11",
"1": "2020-12",
"2": "2021-01",
"3": "2020-11",
"4": "2020-12",
"5": "2021-01",
"6": "2021-02",
"7": "2021-03",
"8": "2021-04",
"9": "2021-05",
"10": "2021-06",
"11": "2021-07",
"12": "2021-08",
"13": "2021-09",
"14": "2021-10",
"15": "2020-11",
"16": "2020-12",
"17": "2021-01",
"18": "2021-02",
"19": "2021-03",
"20": "2021-05",
"21": "2020-11",
"22": "2020-12",
"23": "2021-01",
"24": "2021-02",
"25": "2021-03",
"26": "2021-04",
"27": "2021-05",
"28": "2021-06",
"29": "2021-07",
"30": "2021-08",
"31": "2021-09",
"32": "2021-10",
"33": "2021-06",
"34": "2021-07",
"35": "2021-08",
"36": "2021-09",
"37": "2021-10",
"38": "2021-01",
"39": "2021-02",
"40": "2021-03",
"41": "2021-04",
"42": "2021-05",
"43": "2021-06",
"44": "2021-07",
"45": "2021-08",
"46": "2021-09",
"47": "2021-10",
"48": "2020-11",
"49": "2020-12",
"50": "2021-01",
"51": "2021-02",
"52": "2021-03",
"53": "2021-04",
"54": "2021-05",
"55": "2021-06",
"56": "2021-07",
"57": "2021-08",
"58": "2021-09",
"59": "2021-10",
"60": "2020-11",
"61": "2020-12",
"62": "2021-01",
"63": "2021-01",
"64": "2021-02",
"65": "2021-03"
},
"qtitem": {
"0": 29606.085,
"1": 55669.94,
"2": 12620.795,
"3": 4172.595,
"4": 15973.18,
"5": 29583.265,
"6": 9573.55,
"7": 7481.615,
"8": 9867.61,
"9": 6920.2,
"10": 13302.43,
"11": 7521.93,
"12": 8717.32,
"13": 28167.175,
"14": 233.19,
"15": 8782.49,
"16": 21487.606,
"17": 24928.455,
"18": 25881.984,
"19": 4518.91,
"20": 30.91,
"21": 3135.917,
"22": 17780.43,
"23": 15939.77,
"24": 5563.82,
"25": 10907.05,
"26": 17472.74,
"27": 6894.175,
"28": 4822.273,
"29": 7666.97,
"30": 1632.355,
"31": 2350.045,
"32": 1570.725,
"33": 18630.795,
"34": 25282.762,
"35": 10598.477,
"36": 22747.16,
"37": 6733.85,
"38": 13640.498,
"39": 43169.253,
"40": 51986.2,
"41": 69132.064,
"42": 75535.349,
"43": 84728.937,
"44": 77184.106,
"45": 73209.461,
"46": 64919.575,
"47": 20166.399,
"48": 16716.792,
"49": 35756.182,
"50": 23024.191,
"51": 24383.523,
"52": 30234.558,
"53": 26339.483,
"54": 36889.168,
"55": 30157.181,
"56": 29181.089,
"57": 35839.836,
"58": 26130.035,
"59": 7512.16,
"60": 582.43,
"61": 797.311,
"62": 36.0,
"63": 1819.24,
"64": 8321.185,
"65": 1282.0
}
}
# Agora estou reconvertendo o dicionário em DataFrame
movimento_por_mes_vendedores = pd.DataFrame(my_json)
# A partir desta parte o código está igual ao meu aqui.
vendedores = [{'label': x, 'value': x} for x in sorted(movimento_por_mes_vendedores['vendedor'].unique())]
movimento_por_mes_vendedores.set_index('vendedor', inplace = True)
N = 20
database = movimento_por_mes_vendedores
#database = {
# 'index': list(range(N)),
# 'maiores': [randint(1, 1000) for _ in range(N)],
# 'menores': [randint(1, 1000) for _ in range(N)],
# 'bebes': [randint(1, 1000) for _ in range(N)],
#}
for x in sorted(movimento_por_mes_vendedores):
print(database)
app.layout = Div(
children=[
H1('Evento X'),
H3('idade das pessoas que foram ao evento'),
Checklist(
id='meu_check_list',
options=vendedores,
value=['EDSON MARTINS']
),
Dropdown(
id='meu_dropdown',
options=[
{'label': 'Linha', 'value': 'line'},
{'label': 'Barra', 'value': 'bar'},
],
value='line'
),
Graph(
id='meu_grafico',
config={'displayModeBar': False},
)
]
)
@app.callback(
Output('meu_grafico', 'figure'),
[
Input('meu_check_list', 'value'),
Input('meu_dropdown', 'value'),
]
)
def my_callback(input_data, graph_type):
print('input_data', input_data)
grafico = {
'data': []
}
for x in input_data:
print(f'********** y: {database[x]},"x": {database["index"]} , "name": {x},')
grafico['data'].append(
{
'y': database[x],
'x': database['index'],
'name': x,
'type': graph_type
},
)
return grafico
app.run_server(debug=True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment