Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import numpy
- from click import style
- from dash import Dash, html, dcc, callback, Output, Input, State
- import pandas as pd
- import dash_mantine_components as dmc
- import plotly.graph_objects as go
- from dash import dash_table
- import dash_bootstrap_components as dbc
- import plotly.express as px
- import base64
- import datetime
- import io
- import numpy as np
- local_data_frame = pd.read_excel("data2.xlsx")
- app = Dash(__name__, external_stylesheets=[dbc.themes.BOOTSTRAP])
- def generate_capital():
- labels = ['Уставный капитал', 'Добавочный капитал', 'Резервный капитал', 'Нераспределенная прибыль']
- f1310 = local_data_frame.loc[local_data_frame['Код'] == "Ф1.1310"].iloc[:, 3:].to_numpy()[0][0]
- f1340 = local_data_frame.loc[local_data_frame['Код'] == "Ф1.1340"].iloc[:, 3:].to_numpy()[0][0]
- f1350 = local_data_frame.loc[local_data_frame['Код'] == "Ф1.1350"].iloc[:, 3:].to_numpy()[0][0]
- f1370 = local_data_frame.loc[local_data_frame['Код'] == "Ф1.1370"].iloc[:, 3:].to_numpy()[0][0]
- f1360 = local_data_frame.loc[local_data_frame['Код'] == "Ф1.1360"].iloc[:, 3:].to_numpy()[0][0]
- dk = f1340+f1350
- values = list(map(lambda x: 0if x<0 else x,[f1310, dk, f1360, f1370]))
- # print(values)
- fig = go.Figure(data=[go.Pie(labels=labels, values=values, textinfo='label+percent',
- insidetextorientation='radial',
- hole=.7
- )])
- fig.update_layout(plot_bgcolor='white', )
- return fig
- def parse_contents(contents, filename, date):
- content_type, content_string = contents.split(',')
- decoded = base64.b64decode(content_string)
- try:
- if 'csv' in filename:
- data_frame = pd.read_csv(
- io.StringIO(decoded.decode('utf-8')))
- elif 'xlsx' in filename:
- data_frame = pd.read_excel(io.BytesIO(decoded))
- # local_data_frame = data_frame
- except Exception as e:
- return html.Div([
- 'There was an error processing this file.'
- ])
- return [data_frame]
- @callback(Output('output-data-upload', 'data'),
- Input('upload-data', 'contents'),
- State('upload-data', 'filename'),
- State('upload-data', 'last_modified'))
- def update_output(list_of_contents, list_of_names, list_of_dates):
- if list_of_contents is not None:
- children = [
- parse_contents(c, n, d) for c, n, d in
- zip(list_of_contents, list_of_names, list_of_dates)]
- return children[0]
- def generate_obligations():
- # print(f"VREDSV {outputDataUpload}")
- years = years = list(local_data_frame.iloc[:, 3:].columns.values)
- f1400 = local_data_frame.loc[local_data_frame['Код'] == "Ф1.1400"].iloc[:, 3:].to_numpy()[0]
- fig = go.Figure(data=[go.Scatter(x=years, y=f1400)])
- fig.update_layout(plot_bgcolor='white', )
- return fig
- def generate_actives():
- f1240 = local_data_frame.loc[local_data_frame['Код'] == "Ф1.1240"].iloc[:, 3:]
- f1250 = local_data_frame.loc[local_data_frame['Код'] == "Ф1.1250"].iloc[:, 3:]
- f1230 = local_data_frame.loc[local_data_frame['Код'] == "Ф1.1230"].iloc[:, 3:]
- f1210 = local_data_frame.loc[local_data_frame['Код'] == "Ф1.1210"].iloc[:, 3:]
- f1220 = local_data_frame.loc[local_data_frame['Код'] == "Ф1.1220"].iloc[:, 3:]
- f1260 = local_data_frame.loc[local_data_frame['Код'] == "Ф1.1260"].iloc[:, 3:]
- f1100 = local_data_frame.loc[local_data_frame['Код'] == "Ф1.1100"].iloc[:, 3:]
- years = list(local_data_frame.iloc[:, 3:].columns.values)
- years = list(map(lambda year: str(year)+"<br>",years))[:4]
- a1 = f1240.to_numpy() + f1250.to_numpy()
- a2 = f1230.to_numpy()
- a3 = f1210.to_numpy() + f1220.to_numpy() + f1260.to_numpy()
- a4 = f1100.to_numpy()
- print(a1)
- x_data = np.array([a1[0][:4],a2[0][:4],a3[0][:4],a4[0][:4]]).T
- a1 = x_data[:, 0]
- a1 = a1 / sum(a1)
- a1 = numpy.rint((a1 * 100))
- a2 = x_data[:, 1]
- a2 = a2 / sum(a2)
- a2 = numpy.rint((a2 * 100))
- a3 = x_data[:, 2]
- a3 = a3 / sum(a3)
- a3 = numpy.rint((a3 * 100))
- a4 = x_data[:, 3]
- a4 = a4 / sum(a4)
- a4 = numpy.rint((a4 * 100))
- x_data = np.array([a1,a2,a3,a4]).T
- return generate_smart_grid( top_labels=['A1', 'A2', 'A3', 'A4' ],
- x_data=x_data,
- y_data=years,
- colors=['rgba(38, 24, 74, 0.8)',
- 'rgba(71, 58, 131, 0.8)',
- 'rgba(122, 120, 168, 0.8)',
- 'rgba(164, 163, 204, 0.85)',
- 'rgba(190, 192, 213, 1)'],)
- def generate_passivities():
- f1520 = local_data_frame.loc[local_data_frame['Код'] == "Ф1.1520"].iloc[:, 3:]
- f1510 = local_data_frame.loc[local_data_frame['Код'] == "Ф1.1510"].iloc[:, 3:]
- f1540 = local_data_frame.loc[local_data_frame['Код'] == "Ф1.1540"].iloc[:, 3:]
- f1550 = local_data_frame.loc[local_data_frame['Код'] == "Ф1.1550"].iloc[:, 3:]
- f1400 = local_data_frame.loc[local_data_frame['Код'] == "Ф1.1400"].iloc[:, 3:]
- f1300 = local_data_frame.loc[local_data_frame['Код'] == "Ф1.1300"].iloc[:, 3:]
- years = list(local_data_frame.iloc[:, 3:].columns.values)
- years = list(map(lambda year: str(year) + "<br>", years))[:4]
- p1 = f1520.to_numpy()
- p2 = f1510.to_numpy()+f1540.to_numpy()+f1550.to_numpy()
- p3 = f1400.to_numpy()
- p4 = f1300.to_numpy()
- x_data = np.array([p1[0][:4], p2[0][:4], p3[0][:4], p4[0][:4]]).T
- p1 = x_data[:, 0]
- p1 = p1 / sum(p1)
- p1 = numpy.rint((p1 * 100))
- p2 = x_data[:, 1]
- p2 = p2 / sum(p2)
- p2 = numpy.rint((p2 * 100))
- p3 = x_data[:, 2]
- p3 = p3 / sum(p3)
- p3 = numpy.rint((p3 * 100))
- p4 = x_data[:, 3]
- p4 = p4 / sum(p4)
- p4 = numpy.rint((p4 * 100))
- x_data = np.array([p1, p2, p3, p4]).T
- return generate_smart_grid( top_labels=['П1', 'П2', 'П3', 'П4', ],
- x_data=x_data,
- y_data=years,
- colors=['rgba(169, 29, 17, 0.5)',
- 'rgba(239, 48, 56, 0.6)',
- 'rgba(255, 64, 64, 0.7)',
- 'rgba(246, 74, 70, 0.8)',
- 'rgba(246, 74, 70, 1)'],)
- def generate_smart_grid(top_labels, colors, x_data, y_data):
- fig = go.Figure()
- for i in range(0, len(x_data[0])):
- for xd, yd in zip(x_data, y_data):
- fig.add_trace(go.Bar(
- x=[xd[i]], y=[yd],
- orientation='h',
- marker=dict(
- color=colors[i],
- line=dict(color='rgb(248, 248, 249)', width=1)
- )
- ))
- fig.update_layout(
- xaxis=dict(
- showgrid=False,
- showline=False,
- showticklabels=False,
- zeroline=False,
- domain=[0.15, 1]
- ),
- yaxis=dict(
- showgrid=False,
- showline=False,
- showticklabels=False,
- zeroline=False,
- ),
- barmode='stack',
- # paper_bgcolor='rgb(248, 248, 255)',
- plot_bgcolor='white',
- margin=dict(l=0, r=10, t=140, b=80),
- showlegend=False,
- )
- annotations = []
- for yd, xd in zip(y_data, x_data):
- # labeling the y-axis
- annotations.append(dict(xref='paper', yref='y',
- x=0.14, y=yd,
- xanchor='right',
- text=str(yd),
- font=dict(family='Arial', size=14,
- color='rgb(67, 67, 67)'),
- showarrow=False, align='right'))
- # labeling the first percentage of each bar (x_axis)
- annotations.append(dict(xref='x', yref='y',
- x=xd[0] / 2, y=yd,
- text=str(xd[0]) + '%',
- font=dict(family='Arial', size=14,
- color='rgb(248, 248, 255)'),
- showarrow=False))
- # labeling the first Likert scale (on the top)
- if yd == y_data[-1]:
- annotations.append(dict(xref='x', yref='paper',
- x=xd[0] / 2, y=1.1,
- text=top_labels[0],
- font=dict(family='Arial', size=14,
- color='rgb(67, 67, 67)'),
- showarrow=False))
- space = xd[0]
- for i in range(1, len(xd)):
- # labeling the rest of percentages for each bar (x_axis)
- annotations.append(dict(xref='x', yref='y',
- x=space + (xd[i] / 2), y=yd,
- text=str(xd[i]) + '%',
- font=dict(family='Arial', size=14,
- color='rgb(248, 248, 255)'),
- showarrow=False))
- # labeling the Likert scale
- if yd == y_data[-1]:
- annotations.append(dict(xref='x', yref='paper',
- x=space + (xd[i] / 2), y=1.1,
- text=top_labels[i],
- font=dict(family='Arial', size=14,
- color='rgb(67, 67, 67)'),
- showarrow=False))
- space += xd[i]
- fig.update_layout(annotations=annotations)
- return fig
- def generate_net_profit():
- f1370 = list(local_data_frame.loc[local_data_frame['Код'] == "Ф1.1370"].iloc[:, 3:].to_numpy()[0])
- cp = []
- l = len(f1370)
- for i in range(len(f1370)):
- if(i+1<l):
- cp.append(f1370[i]-f1370[i+1])
- years = list(local_data_frame.iloc[:, 3:].columns.values)
- y = len(cp)
- years = years[:y]
- colors = ['lightslategray', ] * len(years)
- for i in range(len(cp)):
- if cp[i] < 0:
- colors[i] = 'crimson'
- fig = go.Figure(data=[go.Bar(
- x=years,
- y=cp,
- marker_color=colors # marker color can be a single color value or an iterable
- )])
- fig.update_layout(plot_bgcolor='white', )
- # df = px.data.iris()
- # fig = px.scatter_3d(df, x='sepal_length', y='sepal_width', z='petal_width',
- # color='petal_length', symbol='species')
- return fig
- app.layout = html.Div(children=[
- html.Div([
- dcc.Upload(
- id='upload-data',
- children=html.Div([
- 'Перетащите файл или',
- html.A(' Выберите файл')
- ]),
- style={
- 'width': '100%',
- 'height': '60px',
- 'lineHeight': '60px',
- 'borderWidth': '1px',
- 'borderStyle': 'dashed',
- 'borderRadius': '5px',
- 'textAlign': 'center',
- 'margin': '10px'
- },
- multiple=True
- ),
- ]),
- dcc.Store(id="output-data-upload",),
- html.Div(id="main_container",)
- ])
- @callback(Output('main_container', 'children'),
- Input('output-data-upload','data',),)
- def build_main_container(data):
- return html.Div(children=[
- dbc.Container([
- html.Br(),
- html.H1(children='Бизнес-аналитика', style={'textAlign': 'center'}),
- html.Br(),
- dbc.Row(style={"paddingBottom": 18}, children=[
- # dbc.Col(dbc.Card(children=[dbc.Row(), ]), md=2),
- dbc.Col(dbc.Card(style={"border": 0}, children=[html.Br(), dbc.Row(dmc.Text(
- "Капитал",
- size="lg",
- color="dimmed",
- align="center"
- )),
- dcc.Graph(id="capital_graph", figure=generate_capital())
- ]), md=6),
- dbc.Col(dbc.Card(style={"border": 0}, children=[html.Br(), dbc.Row(dmc.Text(
- "Чистая прибыль",
- size="lg",
- color="dimmed",
- align="center"
- )),
- dcc.Graph(id="generate_net_profit",
- figure=generate_net_profit())
- ]), md=6)
- ], ),
- dbc.Row(children=[ dbc.Col(dbc.Card(style={"border": 0}, children=[html.Br(), dbc.Row(dmc.Text(
- "Активы",
- size="lg",
- color="dimmed",
- align="center"
- )),
- dcc.Graph(id="generate_actives", figure=generate_actives())
- ]), md=12)],style={"paddingBottom": 18}, ),
- dbc.Row(children=[dbc.Col(dbc.Card(style={"border": 0}, children=[html.Br(), dbc.Row(dmc.Text(
- "Пассивы",
- size="lg",
- color="dimmed",
- align="center"
- )),
- dcc.Graph(id="generate_passivities",
- figure=generate_passivities())
- ]), md=12)],style={"paddingBottom": 18}, ),
- dbc.Row(children=[
- dbc.Col(dbc.Card(style={"border": 0}, children=[html.Br(), dbc.Row(dmc.Text(
- "Долгосрочные обязательства",
- size="lg",
- color="dimmed",
- align="center"
- ))
- , dcc.Graph(id="generate_obligations",figure=generate_obligations())
- ]), md=12),
- ], ),
- # dcc.Store(id='output-data-upload', ),
- ], style={"background": "#F7F7F8", "margin": 0}, fluid=True),
- html.Div(children=[dash_table.DataTable(
- local_data_frame.to_dict('records'),
- [{'name': str(i), 'id': str(i)} for i in local_data_frame.columns]
- )]),
- ])
- if __name__ == '__main__':
- app.run_server(debug=True,threaded=True,)
- # print( df.to_markdown())
- f1240 = local_data_frame.loc[local_data_frame['Код'] == "Ф1.1240"].iloc[:, 3:]
- f1250 = local_data_frame.loc[local_data_frame['Код'] == "Ф1.1250"].iloc[:, 3:]
- f1230 = local_data_frame.loc[local_data_frame['Код'] == "Ф1.1230"].iloc[:, 3:]
- f1210 = local_data_frame.loc[local_data_frame['Код'] == "Ф1.1210"].iloc[:, 3:]
- f1220 = local_data_frame.loc[local_data_frame['Код'] == "Ф1.1220"].iloc[:, 3:]
- f1260 = local_data_frame.loc[local_data_frame['Код'] == "Ф1.1260"].iloc[:, 3:]
- f1100 = local_data_frame.loc[local_data_frame['Код'] == "Ф1.1100"].iloc[:, 3:]
- f1370= local_data_frame.loc[local_data_frame['Код'] == "Ф1.1370"].iloc[:, 3:]
- # years = list(df.iloc[:, 3:].columns.values)
- # years = list(map(lambda year: str(year) + "<br>", years))
- # a1 = f1240.to_numpy() + f1250.to_numpy()
- # a2 = f1230.to_numpy()
- # a3 = f1210.to_numpy() + f1220.to_numpy() + f1260.to_numpy()
- # a4 = f1100.to_numpy()
- # x_data = np.array([a1[0][:4], a2[0][:4], a3[0][:4], a4[0][:4]]).T
- # print(x_data)
- # a1 = x_data[:,0]
- # a1 = a1/sum(a1)
- # a1 = numpy.rint((a1*100))
- # print(x_data[:,0])
- # print(numpy.rint((a1*100)))
Add Comment
Please, Sign In to add comment