Hi friends.
My code is
import dash
import dash_html_components as html
import dash_core_components as dcc
import plotly.graph_objs as go
import pandas as pd
from plotly import tools
external_stylesheets = ['https://codepen.io/chriddyp/pen/bWLwgP.css']
app = dash.Dash(__name__, external_stylesheets=external_stylesheets)
app.scripts.config.serve_locally=True
server = app.server
data_2019 = pd.read_csv('data_2019.csv', sep=';')
df_2019_based_on_leads = data_2019[data_2019['Лиды/Всего']>0] # ЛИДЫ БОЛЬШЕ 0
def create_table(currency):
eur_df_2019_based_on_leads = df_2019_based_on_leads[df_2019_based_on_leads['Валюта']==currency] # ТОЛЬКО В ЕВРО
eur_df_2019_based_on_leads_revenue = eur_df_2019_based_on_leads[['Дата','ID Партнера','Доходы']]
eur_df_2019_based_on_leads_revenue = eur_df_2019_based_on_leads_revenue[
eur_df_2019_based_on_leads_revenue['Доходы']>0].groupby(['ID Партнера','Дата']
)['Доходы'].sum().reset_index() # ПЕРЕФОРМАТИРОВАНИЕ ПОД ДОХОД
return eur_df_2019_based_on_leads_revenue
def tops_wm(currency):
df = create_table(currency)
tops = df.groupby('ID Партнера')['Доходы'].sum().sort_values(ascending=False).reset_index()
return tops
eur_df = pd.pivot_table(create_table('EUR'),
index='Дата',
values='Доходы',
columns='ID Партнера',
fill_value=0)
pln_df = pd.pivot_table(create_table('PLN'),
index='Дата',
values='Доходы',
columns='ID Партнера',
fill_value=0)
usd_df = pd.pivot_table(create_table('USD'),
index='Дата',
values='Доходы',
columns='ID Партнера',
fill_value=0)
mxn_df = pd.pivot_table(create_table('MXN'),
index='Дата',
values='Доходы',
columns='ID Партнера',
fill_value=0)
eur_top = tops_wm('EUR')
pln_top = tops_wm('PLN')
usd_top = tops_wm('USD')
mxn_top = tops_wm('MXN')
traces1 = []
traces2 = []
traces3 = []
traces4 = []
def generate_data_for_graph(selected_top_count):
e_columns = eur_top.iloc[selected_top_count[0]:selected_top_count[1]+1]['ID Партнера'].values
p_columns = pln_top.iloc[selected_top_count[0]:selected_top_count[1]+1]['ID Партнера'].values
u_columns = usd_top.iloc[selected_top_count[0]:selected_top_count[1]+1]['ID Партнера'].values
m_columns = mxn_top.iloc[selected_top_count[0]:selected_top_count[1]+1]['ID Партнера'].values
e_df = eur_df[e_columns]
p_df = pln_df[p_columns]
u_df = usd_df[u_columns]
m_df = mxn_df[m_columns]
for i in e_df.columns:
traces1.append(go.Scatter(
x=e_df.index,
y=e_df[i].values,
name=str(i)
))
for i in p_df.columns:
traces2.append(go.Scatter(
x=p_df.index,
y=p_df[i].values,
name=str(i)
))
for i in u_df.columns:
traces3.append(go.Scatter(
x=p_df.index,
y=p_df[i].values,
name=str(i)
))
for i in m_df.columns:
traces4.append(go.Scatter(
x=m_df.index,
y=m_df[i].values,
name=str(i)
))
data = tools.make_subplots(rows=3, cols=1)
data.append_trace(traces4, 1, 1)
data.append_trace(traces3, 1, 1)
data.append_trace(traces2, 2, 1)
data.append_trace(traces1, 3, 1)
return data
app.layout = html.Div([
html.Div(id='EUR'[dcc.Graph(id='graph'), # ВЫВОД ГРАФИКА
dcc.RangeSlider(
id='tops',
min=0,
max=10, # eur_top.index.max(),
value=[0,0],
# marks={i: 'Топ: {}'.format(i+1) for i in eur_top.index}
)
])
])
@app.callback(
dash.dependencies.Output('graph', 'figure'),
[dash.dependencies.Input('tops', 'value')])
def update_figure(selected_top_count):
generate_data_for_graph(selected_top_count)
return {
'data': data
# 'layout': go.Layout(
# title='US Export of Plastic Scrap',
# showlegend=True,
# legend=go.layout.Legend(
# x=0,
# y=1.0
# ),
# margin=go.layout.Margin(l=40, r=40, t=40, b=30)
# )
}
if __name__ == '__main__':
app.run_server(debug=True)
i’m getting error:
"Traceback (most recent call last):
File "try_graphs.py", line 159, in <module>
value=[0,0],
TypeError: string indices must be integers
I cant find the problem, coz wen i do the same code without functions - i dont get this error. And i need this functions.
What am i doing wrong?