Update app.py
Browse files
app.py
CHANGED
@@ -1,17 +1,21 @@
|
|
1 |
import faicons as fa
|
2 |
import plotly.express as px
|
|
|
3 |
|
4 |
-
#
|
5 |
-
from shared import app_dir, tips
|
6 |
from shinywidgets import render_plotly
|
7 |
-
|
8 |
from shiny import reactive, render
|
9 |
from shiny.express import input, ui
|
10 |
|
|
|
|
|
|
|
|
|
11 |
bill_rng = (min(tips.سن), max(tips.سن))
|
12 |
|
13 |
-
#
|
14 |
-
ui.page_opts(title="
|
15 |
|
16 |
with ui.sidebar(open="desktop"):
|
17 |
ui.input_slider(
|
@@ -21,18 +25,17 @@ with ui.sidebar(open="desktop"):
|
|
21 |
max=bill_rng[1],
|
22 |
value=bill_rng,
|
23 |
pre="سال"
|
24 |
-
|
25 |
)
|
26 |
ui.input_checkbox_group(
|
27 |
"تاریخ",
|
28 |
-
"
|
29 |
-
["
|
30 |
-
selected=["
|
31 |
inline=True,
|
32 |
)
|
33 |
-
ui.input_action_button("reset", "
|
34 |
|
35 |
-
#
|
36 |
ICONS = {
|
37 |
"user": fa.icon_svg("user", "regular"),
|
38 |
"wallet": fa.icon_svg("wallet"),
|
@@ -40,9 +43,10 @@ ICONS = {
|
|
40 |
"ellipsis": fa.icon_svg("ellipsis"),
|
41 |
}
|
42 |
|
|
|
43 |
with ui.layout_columns(fill=False):
|
44 |
with ui.value_box(showcase=ICONS["user"]):
|
45 |
-
"
|
46 |
|
47 |
@render.express
|
48 |
def total_tippers():
|
@@ -50,55 +54,57 @@ with ui.layout_columns(fill=False):
|
|
50 |
return data.shape[0]
|
51 |
|
52 |
with ui.value_box(showcase=ICONS["wallet"]):
|
53 |
-
"
|
54 |
|
55 |
@render.express
|
56 |
def average_tip():
|
57 |
data = tips_data()
|
58 |
if data.shape[0] > 0:
|
59 |
-
|
60 |
-
return f"{perc.mean():.1%}"
|
61 |
|
62 |
with ui.value_box(showcase=ICONS["currency-dollar"]):
|
63 |
-
"
|
64 |
|
65 |
@render.express
|
66 |
def average_bill():
|
67 |
data = tips_data()
|
68 |
if data.shape[0] > 0:
|
69 |
-
|
70 |
-
return f"${bill:.2f}"
|
71 |
|
|
|
72 |
with ui.layout_columns(col_widths=[6, 6, 12]):
|
73 |
with ui.card(full_screen=True):
|
74 |
-
ui.card_header("
|
75 |
|
76 |
@render.data_frame
|
77 |
def table():
|
78 |
-
return tips_data()
|
79 |
-
|
80 |
@render_plotly
|
81 |
def scatterplot():
|
82 |
-
data = tips_data()
|
83 |
if data.shape[0] == 0:
|
84 |
-
return {}
|
85 |
return px.scatter(
|
86 |
data,
|
87 |
-
x="سن",
|
88 |
-
y="
|
89 |
-
color=
|
90 |
-
trendline="lowess",
|
|
|
|
|
91 |
)
|
|
|
92 |
with ui.card(full_screen=True):
|
93 |
with ui.card_header(class_="d-flex justify-content-between align-items-center"):
|
94 |
-
"
|
95 |
-
with ui.popover(title="
|
96 |
ICONS["ellipsis"]
|
97 |
ui.input_radio_buttons(
|
98 |
"tip_perc_y",
|
99 |
-
"
|
100 |
-
[
|
101 |
-
selected="
|
102 |
inline=True,
|
103 |
)
|
104 |
|
@@ -108,9 +114,9 @@ with ui.layout_columns(col_widths=[6, 6, 12]):
|
|
108 |
|
109 |
dat = tips_data()
|
110 |
if dat.shape[0] == 0:
|
111 |
-
return {}
|
112 |
|
113 |
-
dat["percent"] = dat
|
114 |
yvar = input.tip_perc_y()
|
115 |
uvals = dat[yvar].unique()
|
116 |
|
@@ -132,27 +138,25 @@ with ui.layout_columns(col_widths=[6, 6, 12]):
|
|
132 |
|
133 |
return plt
|
134 |
|
135 |
-
|
136 |
ui.include_css(app_dir / "styles.css")
|
137 |
|
138 |
# --------------------------------------------------------
|
139 |
-
#
|
140 |
# --------------------------------------------------------
|
141 |
|
142 |
-
|
143 |
@reactive.calc
|
144 |
def tips_data():
|
145 |
-
سنی = input.سن()
|
146 |
-
تاریخ_انتخابی = input
|
147 |
-
|
148 |
-
idx1 = tips["سن"].between(سنی[0], سنی[1]) # فیلتر بر اساس رنج سنی
|
149 |
-
idx2 = tips["تاریخ"].isin(tاریخ_انتخابی) # فیلتر بر اساس تاریخ انتخابی
|
150 |
-
filtered_data = tips[idx1 & idx2] # دادههای فیلتر شده
|
151 |
|
152 |
-
|
|
|
|
|
|
|
153 |
|
154 |
@reactive.effect
|
155 |
@reactive.event(input.reset)
|
156 |
def _():
|
157 |
-
ui.update_slider("
|
158 |
-
ui.update_checkbox_group("تاریخ", selected=["
|
|
|
1 |
import faicons as fa
|
2 |
import plotly.express as px
|
3 |
+
import pandas as pd
|
4 |
|
5 |
+
# بارگذاری دادهها
|
6 |
+
from shared import app_dir, tips # فرض بر این است که tips از قبل DataFrame است
|
7 |
from shinywidgets import render_plotly
|
|
|
8 |
from shiny import reactive, render
|
9 |
from shiny.express import input, ui
|
10 |
|
11 |
+
# تبدیل احساس به عدد (تحلیل احساسات)
|
12 |
+
tips["tip"] = tips["احساس"].map({"مثبت": 1, "خنثی": 0, "منفی": -1})
|
13 |
+
|
14 |
+
# تعیین بازه سنی
|
15 |
bill_rng = (min(tips.سن), max(tips.سن))
|
16 |
|
17 |
+
# صفحه و سایدبار
|
18 |
+
ui.page_opts(title="تحلیل احساسات کاربران", fillable=True)
|
19 |
|
20 |
with ui.sidebar(open="desktop"):
|
21 |
ui.input_slider(
|
|
|
25 |
max=bill_rng[1],
|
26 |
value=bill_rng,
|
27 |
pre="سال"
|
|
|
28 |
)
|
29 |
ui.input_checkbox_group(
|
30 |
"تاریخ",
|
31 |
+
"شبکه اجتماعی",
|
32 |
+
tips["شبکه اجتماعی"].unique().tolist(),
|
33 |
+
selected=tips["شبکه اجتماعی"].unique().tolist(),
|
34 |
inline=True,
|
35 |
)
|
36 |
+
ui.input_action_button("reset", "بازنشانی فیلتر")
|
37 |
|
38 |
+
# آیکونها
|
39 |
ICONS = {
|
40 |
"user": fa.icon_svg("user", "regular"),
|
41 |
"wallet": fa.icon_svg("wallet"),
|
|
|
43 |
"ellipsis": fa.icon_svg("ellipsis"),
|
44 |
}
|
45 |
|
46 |
+
# باکسهای آماری
|
47 |
with ui.layout_columns(fill=False):
|
48 |
with ui.value_box(showcase=ICONS["user"]):
|
49 |
+
"تعداد کاربران"
|
50 |
|
51 |
@render.express
|
52 |
def total_tippers():
|
|
|
54 |
return data.shape[0]
|
55 |
|
56 |
with ui.value_box(showcase=ICONS["wallet"]):
|
57 |
+
"میانگین احساس"
|
58 |
|
59 |
@render.express
|
60 |
def average_tip():
|
61 |
data = tips_data()
|
62 |
if data.shape[0] > 0:
|
63 |
+
return f"{data['tip'].mean():.2f}"
|
|
|
64 |
|
65 |
with ui.value_box(showcase=ICONS["currency-dollar"]):
|
66 |
+
"میانگین سن"
|
67 |
|
68 |
@render.express
|
69 |
def average_bill():
|
70 |
data = tips_data()
|
71 |
if data.shape[0] > 0:
|
72 |
+
return f"{data['سن'].mean():.1f} سال"
|
|
|
73 |
|
74 |
+
# نمودار و جدول
|
75 |
with ui.layout_columns(col_widths=[6, 6, 12]):
|
76 |
with ui.card(full_screen=True):
|
77 |
+
ui.card_header("جدول دادهها")
|
78 |
|
79 |
@render.data_frame
|
80 |
def table():
|
81 |
+
return tips_data()
|
82 |
+
|
83 |
@render_plotly
|
84 |
def scatterplot():
|
85 |
+
data = tips_data()
|
86 |
if data.shape[0] == 0:
|
87 |
+
return {}
|
88 |
return px.scatter(
|
89 |
data,
|
90 |
+
x="سن",
|
91 |
+
y="tip",
|
92 |
+
color="جنسیت",
|
93 |
+
trendline="lowess",
|
94 |
+
labels={"tip": "امتیاز احساس", "سن": "سن"},
|
95 |
+
title="رابطه سن با احساس"
|
96 |
)
|
97 |
+
|
98 |
with ui.card(full_screen=True):
|
99 |
with ui.card_header(class_="d-flex justify-content-between align-items-center"):
|
100 |
+
"تحلیل پراکندگی احساس"
|
101 |
+
with ui.popover(title="گروهبندی بر اساس متغیر"):
|
102 |
ICONS["ellipsis"]
|
103 |
ui.input_radio_buttons(
|
104 |
"tip_perc_y",
|
105 |
+
"گروهبندی بر اساس:",
|
106 |
+
["جنسیت", "تأثیر", "سطح تأثیر", "موضوع"],
|
107 |
+
selected="جنسیت",
|
108 |
inline=True,
|
109 |
)
|
110 |
|
|
|
114 |
|
115 |
dat = tips_data()
|
116 |
if dat.shape[0] == 0:
|
117 |
+
return {}
|
118 |
|
119 |
+
dat["percent"] = dat["tip"] # استفاده از tip به عنوان درصد احساس
|
120 |
yvar = input.tip_perc_y()
|
121 |
uvals = dat[yvar].unique()
|
122 |
|
|
|
138 |
|
139 |
return plt
|
140 |
|
141 |
+
# اعمال CSS
|
142 |
ui.include_css(app_dir / "styles.css")
|
143 |
|
144 |
# --------------------------------------------------------
|
145 |
+
# واکنشها
|
146 |
# --------------------------------------------------------
|
147 |
|
|
|
148 |
@reactive.calc
|
149 |
def tips_data():
|
150 |
+
سنی = input.سن()
|
151 |
+
تاریخ_انتخابی = input.تاریخ()
|
|
|
|
|
|
|
|
|
152 |
|
153 |
+
idx1 = tips["سن"].between(سنی[0], سنی[1])
|
154 |
+
idx2 = tips["شبکه اجتماعی"].isin(تاریخ_انتخابی)
|
155 |
+
|
156 |
+
return tips[idx1 & idx2]
|
157 |
|
158 |
@reactive.effect
|
159 |
@reactive.event(input.reset)
|
160 |
def _():
|
161 |
+
ui.update_slider("سن", value=bill_rng)
|
162 |
+
ui.update_checkbox_group("تاریخ", selected=tips["شبکه اجتماعی"].unique().tolist())
|