Spaces:
Sleeping
Sleeping
MarcSkovMadsen
commited on
Commit
•
a79cff1
1
Parent(s):
2703250
Update app.py
Browse files
app.py
CHANGED
@@ -1,95 +1,82 @@
|
|
1 |
import panel as pn
|
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 |
-
def
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
def
|
48 |
-
|
49 |
-
|
50 |
-
def
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
)
|
68 |
-
|
69 |
-
name="
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
)
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
# We have to bind the below after the above!
|
84 |
-
pn.bind(update_state_changed_count, text_input, submit_task, clear, watch=True)
|
85 |
-
|
86 |
-
status_report = pn.bind(get_state, state_changed_count, tasks.param.objects)
|
87 |
-
|
88 |
-
pn.Column(
|
89 |
-
"## WTG Task List",
|
90 |
-
status_report,
|
91 |
-
pn.Row(text_input, submit_task),
|
92 |
-
tasks,
|
93 |
-
pn.Row(pn.Spacer(), clear),
|
94 |
-
max_width=500,
|
95 |
-
).servable()
|
|
|
1 |
import panel as pn
|
2 |
+
import param
|
3 |
+
import json
|
4 |
+
|
5 |
+
FORM_TEXT = """\
|
6 |
+
<h1>Join Newsletter</h1>
|
7 |
+
|
8 |
+
Get the latest updates and news about Panel.
|
9 |
+
"""
|
10 |
+
|
11 |
+
FORM_ICON = """\
|
12 |
+
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="2"> <path d="M3 7a2 2 0 0 1 2 -2h14a2 2 0 0 1 2 2v10a2 2 0 0 1 -2 2h-14a2 2 0 0 1 -2 -2v-10z"></path> <path d="M3 7l9 6l9 -6"></path> </svg>
|
13 |
+
"""
|
14 |
+
|
15 |
+
class FormState(param.Parameterized):
|
16 |
+
name = param.String(default="", doc="The name of the user.")
|
17 |
+
email = param.String(default="", doc="The email of the user.")
|
18 |
+
message = param.String(default="", label="Message", doc="An optional message from the user")
|
19 |
+
|
20 |
+
is_not_valid = param.Boolean(default=False)
|
21 |
+
validation_errors = param.Dict()
|
22 |
+
validation_message = param.String()
|
23 |
+
|
24 |
+
def __init__(self, **params):
|
25 |
+
params["name"]=params.get("name", "")
|
26 |
+
super().__init__(**params)
|
27 |
+
|
28 |
+
def _validate(self):
|
29 |
+
errors = {}
|
30 |
+
|
31 |
+
if not self.name:
|
32 |
+
errors["name"] = "No *Name* entered."
|
33 |
+
if not self.email:
|
34 |
+
errors["email"] = "No *Email* entered."
|
35 |
+
elif not "@" in self.email or not "." in self.email:
|
36 |
+
errors["email"] = "Not a valid *Email*."
|
37 |
+
|
38 |
+
self.validation_errors=errors
|
39 |
+
self.is_not_valid = bool(errors)
|
40 |
+
self.validation_message = "**Error**. " + " ".join(errors.values())
|
41 |
+
|
42 |
+
def _to_dict(self):
|
43 |
+
return {
|
44 |
+
"name": self.name, "email": self.email, "message": self.message
|
45 |
+
}
|
46 |
+
|
47 |
+
def _reset_to_defaults(self):
|
48 |
+
self.param.update(name=self.param.name.default, email=self.param.email.default, message=self.param.message.default)
|
49 |
+
|
50 |
+
def submit(self, event):
|
51 |
+
self._validate()
|
52 |
+
|
53 |
+
if not self.validation_errors:
|
54 |
+
pn.state.notifications.success(f"Form submitted: {self._to_dict()}", duration=2000)
|
55 |
+
self._reset_to_defaults()
|
56 |
+
|
57 |
+
|
58 |
+
|
59 |
+
def create_form():
|
60 |
+
form_state = FormState()
|
61 |
+
|
62 |
+
header = pn.Row(
|
63 |
+
pn.pane.SVG(FORM_ICON, margin=0, height=80, sizing_mode="fixed"),
|
64 |
+
FORM_TEXT,
|
65 |
+
)
|
66 |
+
|
67 |
+
error_pane = pn.pane.Alert(object=form_state.param.validation_message, visible=form_state.param.is_not_valid, alert_type="danger", stylesheets=["p {margin-bottom: 0}"])
|
68 |
+
|
69 |
+
name_input = pn.widgets.TextInput.from_param(form_state.param.name, name="Name*", placeholder="User Name")
|
70 |
+
email_input = pn.widgets.TextInput.from_param(form_state.param.email, name="Email*", placeholder="Email Address")
|
71 |
+
message_input = pn.widgets.TextAreaInput.from_param(form_state.param["message"], placeholder="An optional message")
|
72 |
+
|
73 |
+
submit_button = pn.widgets.Button(name="Send", on_click=form_state.submit, button_type="primary")
|
74 |
+
|
75 |
+
return pn.Column(header, error_pane, name_input, email_input, message_input, submit_button, sizing_mode="fixed", width=500, styles={"margin": "auto"})
|
76 |
+
|
77 |
+
|
78 |
+
|
79 |
+
pn.extension(notifications=True, design="bootstrap", sizing_mode="stretch_width")
|
80 |
+
|
81 |
+
form = create_form()
|
82 |
+
form.servable()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|