mirror of
https://github.com/Textualize/textual-serve.git
synced 2025-10-17 02:50:37 +03:00
Improve download example with different types of download
This commit is contained in:
@@ -1,27 +1,52 @@
|
||||
import io
|
||||
from textual import on
|
||||
from textual.app import App, ComposeResult
|
||||
from textual.binding import Binding
|
||||
from textual.widgets import Button
|
||||
|
||||
|
||||
class ScreenshotApp(App[None]):
|
||||
BINDINGS = [Binding("s", "deliver_screenshot", "Screenshot")]
|
||||
|
||||
def compose(self) -> ComposeResult:
|
||||
yield Button("Hello, World!")
|
||||
yield Button("screenshot: no filename or mime", id="button-1")
|
||||
yield Button("screenshot: screenshot.svg / open in browser", id="button-2")
|
||||
yield Button("screenshot: screenshot.svg / download", id="button-3")
|
||||
yield Button(
|
||||
"screenshot: screenshot.svg / open in browser / plaintext mime",
|
||||
id="button-4",
|
||||
)
|
||||
|
||||
@on(Button.Pressed)
|
||||
@on(Button.Pressed, selector="#button-1")
|
||||
def on_button_pressed(self) -> None:
|
||||
self.action_deliver_screenshot()
|
||||
screenshot_string = self.export_screenshot()
|
||||
string_io = io.StringIO(screenshot_string)
|
||||
self.deliver_text(string_io)
|
||||
|
||||
def action_deliver_screenshot(self) -> None:
|
||||
@on(Button.Pressed, selector="#button-2")
|
||||
def on_button_pressed_2(self) -> None:
|
||||
screenshot_string = self.export_screenshot()
|
||||
string_io = io.StringIO(screenshot_string)
|
||||
self.deliver_text(
|
||||
string_io, save_filename="screenshot.svg", open_method="browser"
|
||||
)
|
||||
|
||||
@on(Button.Pressed, selector="#button-3")
|
||||
def on_button_pressed_3(self) -> None:
|
||||
screenshot_string = self.export_screenshot()
|
||||
string_io = io.StringIO(screenshot_string)
|
||||
self.deliver_text(
|
||||
string_io, save_filename="screenshot.svg", open_method="download"
|
||||
)
|
||||
|
||||
@on(Button.Pressed, selector="#button-4")
|
||||
def on_button_pressed_4(self) -> None:
|
||||
screenshot_string = self.export_screenshot()
|
||||
string_io = io.StringIO(screenshot_string)
|
||||
self.deliver_text(
|
||||
string_io,
|
||||
save_filename="screenshot.svg",
|
||||
open_method="browser",
|
||||
mime_type="text/plain",
|
||||
)
|
||||
|
||||
|
||||
app = ScreenshotApp()
|
||||
if __name__ == "__main__":
|
||||
|
||||
Reference in New Issue
Block a user