summaryrefslogtreecommitdiffstats
path: root/examples/dialogs
diff options
context:
space:
mode:
Diffstat (limited to 'examples/dialogs')
-rwxr-xr-xexamples/dialogs/button_dialog.py19
-rwxr-xr-xexamples/dialogs/checkbox_dialog.py36
-rwxr-xr-xexamples/dialogs/input_dialog.py17
-rwxr-xr-xexamples/dialogs/messagebox.py16
-rwxr-xr-xexamples/dialogs/password_dialog.py19
-rwxr-xr-xexamples/dialogs/progress_dialog.py47
-rwxr-xr-xexamples/dialogs/radio_dialog.py39
-rwxr-xr-xexamples/dialogs/styled_messagebox.py37
-rwxr-xr-xexamples/dialogs/yes_no_dialog.py17
9 files changed, 247 insertions, 0 deletions
diff --git a/examples/dialogs/button_dialog.py b/examples/dialogs/button_dialog.py
new file mode 100755
index 0000000..7a99b9a
--- /dev/null
+++ b/examples/dialogs/button_dialog.py
@@ -0,0 +1,19 @@
+#!/usr/bin/env python
+"""
+Example of button dialog window.
+"""
+from prompt_toolkit.shortcuts import button_dialog
+
+
+def main():
+ result = button_dialog(
+ title="Button dialog example",
+ text="Are you sure?",
+ buttons=[("Yes", True), ("No", False), ("Maybe...", None)],
+ ).run()
+
+ print(f"Result = {result}")
+
+
+if __name__ == "__main__":
+ main()
diff --git a/examples/dialogs/checkbox_dialog.py b/examples/dialogs/checkbox_dialog.py
new file mode 100755
index 0000000..90be263
--- /dev/null
+++ b/examples/dialogs/checkbox_dialog.py
@@ -0,0 +1,36 @@
+#!/usr/bin/env python
+"""
+Example of a checkbox-list-based dialog.
+"""
+from prompt_toolkit.formatted_text import HTML
+from prompt_toolkit.shortcuts import checkboxlist_dialog, message_dialog
+from prompt_toolkit.styles import Style
+
+results = checkboxlist_dialog(
+ title="CheckboxList dialog",
+ text="What would you like in your breakfast ?",
+ values=[
+ ("eggs", "Eggs"),
+ ("bacon", HTML("<blue>Bacon</blue>")),
+ ("croissants", "20 Croissants"),
+ ("daily", "The breakfast of the day"),
+ ],
+ style=Style.from_dict(
+ {
+ "dialog": "bg:#cdbbb3",
+ "button": "bg:#bf99a4",
+ "checkbox": "#e8612c",
+ "dialog.body": "bg:#a9cfd0",
+ "dialog shadow": "bg:#c98982",
+ "frame.label": "#fcaca3",
+ "dialog.body label": "#fd8bb6",
+ }
+ ),
+).run()
+if results:
+ message_dialog(
+ title="Room service",
+ text="You selected: %s\nGreat choice sir !" % ",".join(results),
+ ).run()
+else:
+ message_dialog("*starves*").run()
diff --git a/examples/dialogs/input_dialog.py b/examples/dialogs/input_dialog.py
new file mode 100755
index 0000000..6235265
--- /dev/null
+++ b/examples/dialogs/input_dialog.py
@@ -0,0 +1,17 @@
+#!/usr/bin/env python
+"""
+Example of an input box dialog.
+"""
+from prompt_toolkit.shortcuts import input_dialog
+
+
+def main():
+ result = input_dialog(
+ title="Input dialog example", text="Please type your name:"
+ ).run()
+
+ print(f"Result = {result}")
+
+
+if __name__ == "__main__":
+ main()
diff --git a/examples/dialogs/messagebox.py b/examples/dialogs/messagebox.py
new file mode 100755
index 0000000..4642b84
--- /dev/null
+++ b/examples/dialogs/messagebox.py
@@ -0,0 +1,16 @@
+#!/usr/bin/env python
+"""
+Example of a message box window.
+"""
+from prompt_toolkit.shortcuts import message_dialog
+
+
+def main():
+ message_dialog(
+ title="Example dialog window",
+ text="Do you want to continue?\nPress ENTER to quit.",
+ ).run()
+
+
+if __name__ == "__main__":
+ main()
diff --git a/examples/dialogs/password_dialog.py b/examples/dialogs/password_dialog.py
new file mode 100755
index 0000000..39d7b9c
--- /dev/null
+++ b/examples/dialogs/password_dialog.py
@@ -0,0 +1,19 @@
+#!/usr/bin/env python
+"""
+Example of an password input dialog.
+"""
+from prompt_toolkit.shortcuts import input_dialog
+
+
+def main():
+ result = input_dialog(
+ title="Password dialog example",
+ text="Please type your password:",
+ password=True,
+ ).run()
+
+ print(f"Result = {result}")
+
+
+if __name__ == "__main__":
+ main()
diff --git a/examples/dialogs/progress_dialog.py b/examples/dialogs/progress_dialog.py
new file mode 100755
index 0000000..1fd3ffb
--- /dev/null
+++ b/examples/dialogs/progress_dialog.py
@@ -0,0 +1,47 @@
+#!/usr/bin/env python
+"""
+Example of a progress bar dialog.
+"""
+import os
+import time
+
+from prompt_toolkit.shortcuts import progress_dialog
+
+
+def worker(set_percentage, log_text):
+ """
+ This worker function is called by `progress_dialog`. It will run in a
+ background thread.
+
+ The `set_percentage` function can be used to update the progress bar, while
+ the `log_text` function can be used to log text in the logging window.
+ """
+ percentage = 0
+ for dirpath, dirnames, filenames in os.walk("../.."):
+ for f in filenames:
+ log_text(f"{dirpath} / {f}\n")
+ set_percentage(percentage + 1)
+ percentage += 2
+ time.sleep(0.1)
+
+ if percentage == 100:
+ break
+ if percentage == 100:
+ break
+
+ # Show 100% for a second, before quitting.
+ set_percentage(100)
+ time.sleep(1)
+
+
+def main():
+ progress_dialog(
+ title="Progress dialog example",
+ text="As an examples, we walk through the filesystem and print "
+ "all directories",
+ run_callback=worker,
+ ).run()
+
+
+if __name__ == "__main__":
+ main()
diff --git a/examples/dialogs/radio_dialog.py b/examples/dialogs/radio_dialog.py
new file mode 100755
index 0000000..94d80e2
--- /dev/null
+++ b/examples/dialogs/radio_dialog.py
@@ -0,0 +1,39 @@
+#!/usr/bin/env python
+"""
+Example of a radio list box dialog.
+"""
+from prompt_toolkit.formatted_text import HTML
+from prompt_toolkit.shortcuts import radiolist_dialog
+
+
+def main():
+ result = radiolist_dialog(
+ values=[
+ ("red", "Red"),
+ ("green", "Green"),
+ ("blue", "Blue"),
+ ("orange", "Orange"),
+ ],
+ title="Radiolist dialog example",
+ text="Please select a color:",
+ ).run()
+
+ print(f"Result = {result}")
+
+ # With HTML.
+ result = radiolist_dialog(
+ values=[
+ ("red", HTML('<style bg="red" fg="white">Red</style>')),
+ ("green", HTML('<style bg="green" fg="white">Green</style>')),
+ ("blue", HTML('<style bg="blue" fg="white">Blue</style>')),
+ ("orange", HTML('<style bg="orange" fg="white">Orange</style>')),
+ ],
+ title=HTML("Radiolist dialog example <reverse>with colors</reverse>"),
+ text="Please select a color:",
+ ).run()
+
+ print(f"Result = {result}")
+
+
+if __name__ == "__main__":
+ main()
diff --git a/examples/dialogs/styled_messagebox.py b/examples/dialogs/styled_messagebox.py
new file mode 100755
index 0000000..3f6fc53
--- /dev/null
+++ b/examples/dialogs/styled_messagebox.py
@@ -0,0 +1,37 @@
+#!/usr/bin/env python
+"""
+Example of a style dialog window.
+All dialog shortcuts take a `style` argument in order to apply a custom
+styling.
+
+This also demonstrates that the `title` argument can be any kind of formatted
+text.
+"""
+from prompt_toolkit.formatted_text import HTML
+from prompt_toolkit.shortcuts import message_dialog
+from prompt_toolkit.styles import Style
+
+# Custom color scheme.
+example_style = Style.from_dict(
+ {
+ "dialog": "bg:#88ff88",
+ "dialog frame-label": "bg:#ffffff #000000",
+ "dialog.body": "bg:#000000 #00ff00",
+ "dialog shadow": "bg:#00aa00",
+ }
+)
+
+
+def main():
+ message_dialog(
+ title=HTML(
+ '<style bg="blue" fg="white">Styled</style> '
+ '<style fg="ansired">dialog</style> window'
+ ),
+ text="Do you want to continue?\nPress ENTER to quit.",
+ style=example_style,
+ ).run()
+
+
+if __name__ == "__main__":
+ main()
diff --git a/examples/dialogs/yes_no_dialog.py b/examples/dialogs/yes_no_dialog.py
new file mode 100755
index 0000000..4b08dd6
--- /dev/null
+++ b/examples/dialogs/yes_no_dialog.py
@@ -0,0 +1,17 @@
+#!/usr/bin/env python
+"""
+Example of confirmation (yes/no) dialog window.
+"""
+from prompt_toolkit.shortcuts import yes_no_dialog
+
+
+def main():
+ result = yes_no_dialog(
+ title="Yes/No dialog example", text="Do you want to confirm?"
+ ).run()
+
+ print(f"Result = {result}")
+
+
+if __name__ == "__main__":
+ main()