Create utils.py
Browse files
utils.py
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import plotly.graph_objects as go
|
| 2 |
+
import streamlit
|
| 3 |
+
|
| 4 |
+
|
| 5 |
+
@streamlit.cache_data
|
| 6 |
+
def plot_sankey(data, output_file='sankey_diagram_oct.html', i=0, do='до'):
|
| 7 |
+
labels = []
|
| 8 |
+
sources = []
|
| 9 |
+
targets = []
|
| 10 |
+
values = []
|
| 11 |
+
|
| 12 |
+
# Сбор данных для диаграммы
|
| 13 |
+
for event, next_events in data.items():
|
| 14 |
+
# Добавляем основное событие в метки
|
| 15 |
+
if event not in labels:
|
| 16 |
+
labels.append(event)
|
| 17 |
+
|
| 18 |
+
current_event_index = labels.index(event)
|
| 19 |
+
|
| 20 |
+
for next_event, (user_count, percentage) in next_events.items():
|
| 21 |
+
if next_event not in labels:
|
| 22 |
+
labels.append(next_event)
|
| 23 |
+
|
| 24 |
+
next_event_index = labels.index(next_event)
|
| 25 |
+
sources.append(current_event_index)
|
| 26 |
+
targets.append(next_event_index)
|
| 27 |
+
values.append(user_count)
|
| 28 |
+
|
| 29 |
+
# Создание диаграммы
|
| 30 |
+
fig = go.Figure(data=[go.Sankey(
|
| 31 |
+
valueformat= ".0f",
|
| 32 |
+
valuesuffix="TWh",
|
| 33 |
+
|
| 34 |
+
# font = dict(family='Arial',
|
| 35 |
+
# size=14,
|
| 36 |
+
# color='black'),
|
| 37 |
+
node=dict(
|
| 38 |
+
# family='Arial',
|
| 39 |
+
pad=15,
|
| 40 |
+
thickness=20,
|
| 41 |
+
line=dict(color="black", width=0.1),
|
| 42 |
+
label=labels,
|
| 43 |
+
# font = dict(size=15, color="black")
|
| 44 |
+
),
|
| 45 |
+
link=dict(
|
| 46 |
+
# family = 'Arial',
|
| 47 |
+
source=sources,
|
| 48 |
+
target=targets,
|
| 49 |
+
value=values,
|
| 50 |
+
label=[f"{user_count} ({percentage})" for user_count, percentage in
|
| 51 |
+
zip(values, [data[labels[sources[i]]][labels[targets[i]]][1] for i in range(len(sources))])],
|
| 52 |
+
)
|
| 53 |
+
)])
|
| 54 |
+
|
| 55 |
+
return fig.update_layout(title_text=f"Воронка сервиса Финансовое здоровье за октябрь 2024, часть {i} ({do} ГЭС ФЗ)",
|
| 56 |
+
font_size=10,
|
| 57 |
+
font= dict(size=10, color='white'),
|
| 58 |
+
plot_bgcolor='white',
|
| 59 |
+
paper_bgcolor='white')
|
| 60 |
+
|
| 61 |
+
# Сохранение диаграммы в HTML файл
|
| 62 |
+
# fig.write_html(f'test_sanki_oct_part{i}.html')
|
| 63 |
+
# print(f"Диаграмма успешно сохранена в файл: test_sanki.html")
|
| 64 |
+
# fig.show()
|