forked from javelit/javelit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWidgetPersistenceMultiPage.java
More file actions
64 lines (58 loc) · 2.68 KB
/
Copy pathWidgetPersistenceMultiPage.java
File metadata and controls
64 lines (58 loc) · 2.68 KB
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
/// usr/bin/env jbang "$0" "$@" ; exit $?
//DEPS io.javelit:javelit:0.76.0
import java.util.List;
import io.javelit.core.Jt;
// contrary to Streamlit (https://github.com/streamlit/streamlit/issues/6074), Javelit can maintain state of
// widgets across pages. States are maintained if a key is provided with .key()
// To clear all states of a page when the page is left, call .noPersistWhenLeft() on the page.
public class WidgetPersistenceMultiPage {
public static void main(String[] args) {
var page = Jt
.navigation(Jt.page("/page1", () -> page1()).section("hehe"),
Jt.page("/page2", () -> page2()).noPersistWhenLeft())
.use();
page.run();
}
public static void page1() {
Jt.title("Page 1 - Persisted values remain when the page is left").use();
Jt.markdown("Page initialization: `Jt.page(\"/page1\", Page1::app)`").use();
var view = Jt.radio("View", List.of("view1", "view2")).use();
if ("view1".equals(view)) {
Jt.text("☝️ Enter some text in the 3 inputs, then click on view2 above").use();
Jt.textInput("Not persisted text because no key is provided: \n`Jt.textInput(...).use();`").use();
Jt
.textInput("Persisted text because a key is provided: \n`Jt.textInput(...).key(\"text1\").use();`")
.key("text1")
.use();
Jt
.textInput(
"Not persisted text because a key is provided with noPersist: \n`Jt.textInput(...).key(\"text3\").noPersist().use();`")
.key("text3")
.noPersist()
.use();
} else if ("view2".equals(view)) {
Jt.text("☝️ Now go back to view1 and see if your text is still there").use();
}
}
public static void page2() {
Jt.title("Page 2 - All values are cleared when the page is left").use();
Jt.markdown("Page initialization: `Jt.page(\"/page2\", Page2::app).noPersistWhenLeft()`").use();
var view = Jt.radio("View", List.of("view1", "view2")).use();
if ("view1".equals(view)) {
Jt.text("☝️ Enter some text in the 3 inputs, then click on view2 above").use();
Jt.textInput("Not persisted text because no key is provided: \n`Jt.textInput(...).use();`").use();
Jt
.textInput("Persisted text because a key is provided: \n`Jt.textInput(...).key(\"text1\").use();`")
.key("text1")
.use();
Jt
.textInput(
"Not persisted text because a key is provided with noPersist: \n`Jt.textInput(...).key(\"text3\").noPersist().use();`")
.key("text3")
.noPersist()
.use();
} else if ("view2".equals(view)) {
Jt.text("☝️ Now go back to view1 and see if your text is still there").use();
}
}
}