-
Hi. What is the recommended approach to implement frontend/client-side confirmation using Javascript code such as The use case is as follows: the Python code of the view generates an HTML table with several rows, one per data item (the data items are fetched from another backend). One of the table columns has a delete button / link. When the user clicks the delete link, I want to show a confirmation pop-up to the user first, to avoid that they accidentally delete items. I only want to handle the input event if the user confirmed the prompt (clicking "Yes"). What I already tried (and it did not work) was to send something like
|
Beta Was this translation helpful? Give feedback.
Replies: 14 comments 3 replies
-
Hi @MShekow, Does the popup has to be a Javascript https://getbootstrap.com/docs/5.0/components/modal/ I made a custom package for my company, based on bootstrap3 which defines Popups using bootstrap3 modals. Works like a charm! I currently work on a public package with the same functionality based on bootstrap 5 |
Beta Was this translation helpful? Give feedback.
-
Yes, it could totally be the Bootstrap 5 modal. I'm using Bootstrap 5 anyway. I'll try setting up the modal as explained here (because I need the modal's content to be adaptive, showing the item's name that the user is about to delete). |
Beta Was this translation helpful? Give feedback.
-
@MShekow i made a small prototype without bootstrap5 for you. You just have to add the bootstrap5 specific parts. https://lona-web.org/cookbook/using-traditional-html.html from lona.html import Widget, HTML, H1, Div, Button
from lona import LonaApp, LonaView
app = LonaApp(__file__)
class Popup(Widget):
def __init__(self):
self.nodes = [
Div(
Div(_class='popup-title'),
Div(_class='popup-body'),
Div(
Button('Confirm', _class='confirm'),
Button('Cancel', _class='cancel'),
_class='popup-buttons'
),
_class='popup'
)
]
self.confirm_button = self.query_selector('.confirm')
self.cancel_button = self.query_selector('.cancel')
self.hide()
def hide(self):
self.nodes[0].style['display'] = 'none'
def show(self):
with self.lock:
if 'display' in self.nodes[0].style:
del self.nodes[0].style['display']
def set_title(self, *nodes):
self.nodes[0][0].nodes = list(nodes)
def set_body(self, *nodes):
self.nodes[0][1].nodes = list(nodes)
app.add_static_file('lona/style.css', """
.popup {
border: 2px solid grey;
position: absolute;
top: 0;
left: 0;
margin-left: 25%;
margin-top: 10%;
height: 4em;
width: 50%;
}
.popup .popup-title {
font-size: 130%;
font-weight: bold;
}
.popup .popup-buttons {
float: right;
}
""")
@app.route('/')
class MyLonaView(LonaView):
def handle_request(self, request):
popup = Popup()
show_popup = Button('Show Popup', _id='show-popup')
html = HTML(
H1('Popup Demo'),
show_popup,
popup,
)
while True:
input_event = self.await_click(html=html)
if input_event.node is show_popup:
with html.lock:
popup.set_title('Popup Header')
popup.set_body('Popup Body')
popup.show()
elif input_event.node is popup.confirm_button:
print('confirm was clicked')
popup.hide()
elif input_event.node is popup.cancel_button:
print('cancel was clicked')
popup.hide()
app.run(port=8080)
|
Beta Was this translation helpful? Give feedback.
-
Thank you for the minimal example. I would like the code to be closer to what Bootstrap originally does (to e.g. get the nice fade-animation). The snippet below still has problems: I cannot properly identify which buttons were clicked (from the
Do you have any ideas how to fix these problems? View code:
And the
|
Beta Was this translation helpful? Give feedback.
-
I also realized that with my approach, I have no idea how to access the variable piece of information once the user clicked the "Yes" button (my server code needs to know what the current item was when the user clicked Yes). I might have to use your approach, where this information is stored server-side in the |
Beta Was this translation helpful? Give feedback.
-
Hi @MShekow, i published the current state of https://github.com/lona-web-org/lona-bootstrap-5 for you. I implemented bootstrap5 modals using a Lona frontend widget (https://lona-web.org/end-user-documentation/html.html#frontend-widgets). These are little Javascript snippets that run when a Lona renders a widget. Like this i don't have to wait and https://github.com/lona-web-org/lona-bootstrap-5/blob/master/lona_bootstrap_5/modal.py To see the code in action you can read or run: Click events: |
Beta Was this translation helpful? Give feedback.
-
Thank you very much, I'll look into it. Do you have any suggestions for Problem 2 I mentioned above, which also happens in other places (not related to modal dialogs)? |
Beta Was this translation helpful? Give feedback.
-
Lona patches the behavior of links by default so that if you click on a link in an interactive view, a new view gets started over the websocket. You can override this by adding I tried your example and i am not completely sure if i understand your problem entirely. When i click the link "link-text" i |
Beta Was this translation helpful? Give feedback.
-
@MShekow i made a simple example for you how i would write a view with a table with a delete link for each line from lona import LonaView, LonaApp
from lona.html import HTML, A, CLICK, Table, Tr, Th, Td, H1
app = LonaApp(__file__)
@app.route('/')
class MyLonaView(LonaView):
def handle_request(self, request):
data = [
('Alice', '423243924'),
('Bob', '2121243924'),
('Mallory', '1223423243924'),
]
# setup table
table = Table(
Tr(
Th('Name'),
Th('Phone Number'),
Th('Delete'),
),
)
for index, data_set in enumerate(data):
name, phone_number = data_set
delete_link = A('Delete', href="#", events=[CLICK])
table.append(
Tr(
Td(name),
Td(phone_number),
Td(delete_link),
data_id=str(index),
)
)
# setup html
html = HTML(
H1('Phone Book'),
table,
)
# main loop
while True:
self.show(html)
print(data)
input_event = self.await_click()
# here you would add confirmation popup
tr = input_event.node.parent.parent
# remove data_set from data
data_id = int(tr.attributes['data-id'])
data[data_id] = ()
# remove tr from table
tr.remove()
app.run(port=8080) |
Beta Was this translation helpful? Give feedback.
-
Thanks for checking. The fact that I got two nodes in the Also thank you for the new example you provided. Since I need my view to both regularly refresh data (in the |
Beta Was this translation helpful? Give feedback.
-
Hi @MShekow
Yes you are right, you can access Popup: In the meantime i released the first version of lona-bootstrap-5 which has native support for modals Doing two things at the same time: Lona 1.5 has support for callback based input event handling now. Maybe that helps |
Beta Was this translation helpful? Give feedback.
-
@MShekow In the meantime i added a demo that seems to do pretty much what you are looking for i guess |
Beta Was this translation helpful? Give feedback.
-
@MShekow Does this solve your problem? |
Beta Was this translation helpful? Give feedback.
-
Another small issue that popped up with using the Bootstrap 5 Modal: if I add code such as the following:
for a modal initialized like this: |
Beta Was this translation helpful? Give feedback.
@MShekow In the meantime i added a demo that seems to do pretty much what you are looking for i guess
https://lona-web.org/demos/bootstrap-5-confirmation-popup/index.html