Replies: 2 comments 1 reply
-
I'm afraid I don't understand what it is you're asking for. Are you saying you want to see the logs of a Toga app? Those are all output to the console via stdout/stderr. Are you saying you want to use a logging framework with Toga? Go right ahead - Toga's own output won't use it, but Toga also doesn't generate that much console output. Or are you asking for something else? |
Beta Was this translation helpful? Give feedback.
-
@freakboy3742 I believe the OP wanted to have a way to redirect stdout to a GUI element. I created a working example but it only shows the output after the called function is completed. This works for simple cases that complete quickly, but is problematic when the handler calls functions that are computationally intensive and take some time to complete. I am interested in a "logger on gui" as well and would appreciate some help updating the following code to have stdout redirected in real-time so that users can see the status/progress of long-running CLI processes. #!/usr/bin/env python
"""
Call a function and write all its stdout to the GUI.
"""
import toga
from toga.style import Pack
from toga.style.pack import COLUMN, ROW
import sys
import threading
import time
import io
from contextlib import redirect_stdout
def long_running_cli_program(arg1, arg2, arg3=10, arg4=True):
"""Dummy example function. This function normally imported from another file/module that should remain unmodified.
Does long-running computations with print statements to display status and information."""
for x in range(1, arg3 + 1):
print(f"function status update #{x}")
time.sleep(1)
class CLIWindow(toga.App):
def handler_clear_pressed(self, widget, **kwargs):
self.multiline_input.value = ""
def handler_scroll_to_top(self, widget):
self.multiline_input.scroll_to_top()
def handler_scroll_to_bottom(self, widget):
self.multiline_input.scroll_to_bottom()
def handler_process_data(self, widget):
# Capture output from function call (NOTE this does not show progress until the end)
f = io.StringIO()
with redirect_stdout(f):
app_cli = long_running_cli_program(self.input_path, self.output_path, arg3=5, arg4=False),
self.multiline_input.value = self.multiline_input.value + \
f.getvalue()
return None
def startup(self):
"""Construct and show the Toga application."""
self.input_path = r"./"
self.output_path = r"./"
##### Pack Styles #####
style_flex = Pack(flex=1)
##### Multiline Text Input #####
self.multiline_input = toga.MultilineTextInput(
placeholder="Log:",
value="Initial Text",
style=Pack(flex=1, font_family="monospace", font_size=14),
)
self.multiline_input.readonly = True
# Write some initial info
self.multiline_input.value = "Hello" + '\n' + \
"World" + '\n'
##### Buttons #####
button_clear = toga.Button(
text="Clear", icon=None, on_press=self.handler_clear_pressed, style=style_flex, enabled=True
)
button_scroll_top = toga.Button(
"Top", on_press=self.handler_scroll_to_top, style=style_flex
)
button_scroll_bottom = toga.Button(
"Bottom", on_press=self.handler_scroll_to_bottom, style=style_flex
)
button_process_data = toga.Button(
"Process Data", on_press=self.handler_process_data, style=style_flex
)
##### Box Layouts #####
box_multiline_controls = toga.Box(
children=[
button_scroll_top,
button_scroll_bottom,
button_clear,
],
style=Pack(direction=ROW, padding_bottom=10),
)
main_box = toga.Box(
children=[
box_multiline_controls,
self.multiline_input,
button_process_data,
],
style=Pack(flex=1, direction=COLUMN, padding=1, ),
)
self.main_window = toga.MainWindow(title=self.formal_name)
self.main_window.content = main_box
self.main_window.show()
def main():
return CLIWindow("FormalProgram", "com.author.program")
# Run the gui from this file with CLI
if __name__ == "__main__":
app_gui = main()
app_gui.main_loop() |
Beta Was this translation helpful? Give feedback.
-
What is the problem or limitation you are having?
i want logger out on toga gui, how can i do
Describe the solution you'd like
none
Describe alternatives you've considered
none
Additional context
No response
Beta Was this translation helpful? Give feedback.
All reactions