Skip to content

Commit ca88974

Browse files
authored
Merge pull request #15 from Textualize/save-file
Deliver file
2 parents 7f401c5 + 8b94894 commit ca88974

File tree

11 files changed

+719
-280
lines changed

11 files changed

+719
-280
lines changed

examples/download_screenshot.py

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
import io
2+
from pathlib import Path
3+
from textual import on
4+
from textual.app import App, ComposeResult
5+
from textual.events import DeliveryComplete
6+
from textual.widgets import Button, Input, Label
7+
8+
9+
class ScreenshotApp(App[None]):
10+
def compose(self) -> ComposeResult:
11+
yield Button("screenshot: no filename or mime", id="button-1")
12+
yield Button("screenshot: screenshot.svg / open in browser", id="button-2")
13+
yield Button("screenshot: screenshot.svg / download", id="button-3")
14+
yield Button(
15+
"screenshot: screenshot.svg / open in browser / plaintext mime",
16+
id="button-4",
17+
)
18+
yield Label("Deliver custom file:")
19+
yield Input(id="custom-path-input", placeholder="Path to file...")
20+
21+
@on(Button.Pressed, selector="#button-1")
22+
def on_button_pressed(self) -> None:
23+
screenshot_string = self.export_screenshot()
24+
string_io = io.StringIO(screenshot_string)
25+
self.deliver_text(string_io)
26+
27+
@on(Button.Pressed, selector="#button-2")
28+
def on_button_pressed_2(self) -> None:
29+
screenshot_string = self.export_screenshot()
30+
string_io = io.StringIO(screenshot_string)
31+
self.deliver_text(
32+
string_io, save_filename="screenshot.svg", open_method="browser"
33+
)
34+
35+
@on(Button.Pressed, selector="#button-3")
36+
def on_button_pressed_3(self) -> None:
37+
screenshot_string = self.export_screenshot()
38+
string_io = io.StringIO(screenshot_string)
39+
self.deliver_text(
40+
string_io, save_filename="screenshot.svg", open_method="download"
41+
)
42+
43+
@on(Button.Pressed, selector="#button-4")
44+
def on_button_pressed_4(self) -> None:
45+
screenshot_string = self.export_screenshot()
46+
string_io = io.StringIO(screenshot_string)
47+
self.deliver_text(
48+
string_io,
49+
save_filename="screenshot.svg",
50+
open_method="browser",
51+
mime_type="text/plain",
52+
)
53+
54+
@on(DeliveryComplete)
55+
def on_delivery_complete(self, event: DeliveryComplete) -> None:
56+
self.notify(title="Download complete", message=event.key)
57+
58+
@on(Input.Submitted)
59+
def on_input_submitted(self, event: Input.Submitted) -> None:
60+
path = Path(event.value)
61+
if path.exists():
62+
self.deliver_binary(path)
63+
else:
64+
self.notify(
65+
title="Invalid path",
66+
message="The path does not exist.",
67+
severity="error",
68+
)
69+
70+
71+
app = ScreenshotApp()
72+
if __name__ == "__main__":
73+
app.run()

examples/serve_any.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@
33

44
if __name__ == "__main__":
55
server = Server(sys.argv[1])
6-
server.serve(debug=False)
6+
server.serve(debug=True)

pyproject.toml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ dependencies = [
99
"aiohttp>=3.9.5",
1010
"aiohttp-jinja2>=1.6",
1111
"jinja2>=3.1.4",
12+
"rich",
1213
"textual>=0.66.0",
1314
]
1415
readme = "README.md"
@@ -21,7 +22,9 @@ build-backend = "hatchling.build"
2122
[tool.rye]
2223
managed = true
2324
dev-dependencies = [
24-
"httpx", # required to run the dictionary example
25+
"httpx",
26+
# required to run the dictionary example
27+
"textual-dev>=1.5.1",
2528
]
2629

2730
[tool.hatch.metadata]

requirements-dev.lock

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
-e file:.
1212
aiohttp==3.9.5
1313
# via aiohttp-jinja2
14+
# via textual-dev
1415
# via textual-serve
1516
aiohttp-jinja2==1.6
1617
# via textual-serve
@@ -25,6 +26,8 @@ attrs==23.2.0
2526
certifi==2024.7.4
2627
# via httpcore
2728
# via httpx
29+
click==8.1.7
30+
# via textual-dev
2831
exceptiongroup==1.2.2
2932
# via anyio
3033
frozenlist==1.4.1
@@ -54,22 +57,28 @@ mdit-py-plugins==0.4.1
5457
# via markdown-it-py
5558
mdurl==0.1.2
5659
# via markdown-it-py
60+
msgpack==1.0.8
61+
# via textual-dev
5762
multidict==6.0.5
5863
# via aiohttp
5964
# via yarl
6065
pygments==2.18.0
6166
# via rich
6267
rich==13.7.1
6368
# via textual
69+
# via textual-serve
6470
sniffio==1.3.1
6571
# via anyio
6672
# via httpx
67-
textual==0.74.0
73+
textual==0.78.0
74+
# via textual-dev
6875
# via textual-serve
76+
textual-dev==1.5.1
6977
typing-extensions==4.12.2
7078
# via anyio
7179
# via rich
7280
# via textual
81+
# via textual-dev
7382
uc-micro-py==1.0.3
7483
# via linkify-it-py
7584
yarl==1.9.4

requirements.lock

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@ pygments==2.18.0
4747
# via rich
4848
rich==13.7.1
4949
# via textual
50-
textual==0.74.0
50+
# via textual-serve
51+
textual==0.78.0
5152
# via textual-serve
5253
typing-extensions==4.12.2
5354
# via rich

0 commit comments

Comments
 (0)