-
Notifications
You must be signed in to change notification settings - Fork 0
/
dashboard.py
182 lines (161 loc) · 4.87 KB
/
dashboard.py
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#%%
import vizro.plotly.express as px
from vizro.tables import dash_ag_grid
from dash_ag_grid import AgGrid
from vizro import Vizro
import vizro.models as vm
import polars as pl
from vizro.models.types import capture
import vizro.plotly.express as px
from dash import dcc
#%%
df = (pl
.read_parquet("./Data/News/news_with_topics.parquet")
.to_pandas()
)
#%%
company = "Walmart"
current_news = (
pl.DataFrame(df, schema_overrides={"date_published":pl.Date})
.filter(pl.col("companies")==company)
.with_columns(
text=pl.col("text").str.replace_all(r"\n",r"\n\n")
)
)[0]
# Table news
df_news = (
pl.DataFrame(df, schema_overrides={"date_published":pl.Date})
.filter(
pl.col("companies") == company
)
.select(
["title","date_published", "topics_representation", "companies", "topics", "topic_probability_distribution"]
)
)
# Creating the topics list
# df_topics = (
# pl.DataFrame(df)
# .filter(
# pl.col("companies") == company
# )
# .select(["topics","topics_representation"])
# .explode(["topics","topics_representation"])
# .unique(["topics","topics_representation"])
# .drop_nulls()
# .with_columns(
# topics_representation = pl.col("topics_representation").list.join(" "),
# topics = pl.col("topics").cast(pl.Int8).cast(pl.Utf8))
# )
# dict_topics = [{"label":row[1], "value":row[0]} for row in df_topics.rows()]
df_topics = (
pl.DataFrame(df)
.filter(
pl.col("companies") == company
)
.select(["topics","topics_custom_name","topics_count"])
.explode(["topics","topics_custom_name", "topics_count"])
.unique(["topics","topics_custom_name", "topics_count"])
.drop_nulls()
.sort("topics_count", descending=True)
.with_columns(
topics = pl.col("topics").cast(pl.Int8).cast(pl.Utf8))
)
dict_topics = [{"label":row[1], "value":row[0]} for row in df_topics.rows()]
#%%
s_unique_companies = (
pl.DataFrame(df)
.select("companies")
.unique()
)
dict_unique_companies = [{"label":row[0], "value":row[0]} for row in s_unique_companies.rows()]
text = current_news[0,'text'].replace(r"\n","\n")
#%%
@capture("ag_grid")
def my_custom_aggrid(data_frame, company, topics):
"""Custom ag_grid."""
columnDefs = [
{"field": "title"},
{'field': 'topic_probability_distribution',
'initialSort': 'desc',
'headerName':"Topic Probability"}
]
data_frame = (
pl.DataFrame(data_frame)
.filter(
pl.col("companies") == company,
pl.col("topics").list.contains(int(topics))
)
.select(
["title","date_published", "topics_representation", "companies", "topics", "topic_probability_distribution"]
)
.with_columns(
topic_probability_distribution = (
pl.col("topic_probability_distribution")
.list.get(
pl.col("topics")
.list.eval(pl.element()==int(topics))
.list.arg_max()
)
)
.cast(pl.Float64)
.round(2)
)
.to_pandas()
)
defaults = {
"className": "ag-theme-quartz-dark ag-theme-vizro",
"defaultColDef": {
"resizable": True,
"sortable": True,
"filter": True,
"filterParams": {
"buttons": ["apply", "reset"],
"closeOnApply": True,
},
"flex": 1,
"minWidth": 70,
},
"dashGridOptions": {
"pagination": True,
"paginationAutoPageSize": True,
"paginationPageSizeSelector": False
},
"style": {"height": "100%"},
}
return AgGrid(
columnDefs=columnDefs,
rowData=data_frame.to_dict("records"),
**defaults
)
@capture("action")
def my_custom_action(t: int):
"""Custom action."""
sleep(t)
#%%
page = vm.Page(
layout=vm.Layout(grid=[
[0, 0, 1, 1],
[0, 0, 1, 1]
]
),
title="News",
components=[
vm.Card(
text = f"# {current_news[0,'title']} \n\n **{current_news[0,'date_published']}** \n\n {text} \n\n {current_news[0,'link']}"
),
vm.AgGrid(id = "custom_ag_grid", title="Articles", figure=my_custom_aggrid(data_frame=df, company="Walmart", topics="2")),
],
controls=[
vm.Parameter(
targets=["custom_ag_grid.company"],
selector=vm.Dropdown(title="Company", options=dict_unique_companies),
),
vm.Parameter(
targets=["custom_ag_grid.topics"],
selector=vm.Dropdown(title="Hot topics", options=dict_topics, multi=False)
)
],
)
dashboard = vm.Dashboard(pages=[page])
Vizro().build(dashboard).run()
# %%