Merge pull request #13 from Textualize/open-url

Open url
This commit is contained in:
Darren Burns
2024-07-31 10:39:11 +01:00
committed by GitHub
10 changed files with 334 additions and 258 deletions

View File

@@ -1 +1 @@
3.12.3
3.8.19

View File

@@ -12,7 +12,7 @@ from textual.containers import VerticalScroll
from textual.widgets import Input, Markdown
class DictionaryApp(App):
class DictionaryApp(App[None]):
"""Searches a dictionary API as-you-type."""
CSS_PATH = "dictionary.tcss"

24
examples/open_link.py Normal file
View File

@@ -0,0 +1,24 @@
from textual import on
from textual.app import App, ComposeResult
from textual.widgets import Button
class OpenLink(App[None]):
"""Demonstrates opening a URL in the same tab or a new tab."""
def compose(self) -> ComposeResult:
yield Button("Visit the Textual docs", id="open-link-same-tab")
yield Button("Visit the Textual docs in a new tab", id="open-link-new-tab")
@on(Button.Pressed)
def open_link(self, event: Button.Pressed) -> None:
"""Open the URL in the same tab or a new tab depending on which button was pressed."""
self.open_url(
"https://textual.textualize.io",
new_tab=event.button.id == "open-link-new-tab",
)
app = OpenLink()
if __name__ == "__main__":
app.run()

6
examples/serve_any.py Normal file
View File

@@ -0,0 +1,6 @@
import sys
from textual_serve.server import Server
if __name__ == "__main__":
server = Server(sys.argv[1])
server.serve(debug=False)

View File

@@ -0,0 +1,4 @@
from textual_serve.server import Server
server = Server("python open_link.py")
server.serve(debug=False)

View File

@@ -6,10 +6,10 @@ authors = [
{ name = "Will McGugan", email = "will@textualize.io" }
]
dependencies = [
"textual>=0.66.0",
"aiohttp>=3.9.5",
"aiohttp-jinja2>=1.6",
"jinja2>=3.1.4",
"textual>=0.66.0",
]
readme = "README.md"
requires-python = ">= 3.8"
@@ -20,7 +20,9 @@ build-backend = "hatchling.build"
[tool.rye]
managed = true
dev-dependencies = []
dev-dependencies = [
"httpx", # required to run the dictionary example
]
[tool.hatch.metadata]
allow-direct-references = true

View File

@@ -16,12 +16,28 @@ aiohttp-jinja2==1.6
# via textual-serve
aiosignal==1.3.1
# via aiohttp
anyio==4.4.0
# via httpx
async-timeout==4.0.3
# via aiohttp
attrs==23.2.0
# via aiohttp
certifi==2024.7.4
# via httpcore
# via httpx
exceptiongroup==1.2.2
# via anyio
frozenlist==1.4.1
# via aiohttp
# via aiosignal
h11==0.14.0
# via httpcore
httpcore==1.0.5
# via httpx
httpx==0.27.0
idna==3.7
# via anyio
# via httpx
# via yarl
jinja2==3.1.4
# via aiohttp-jinja2
@@ -45,9 +61,14 @@ pygments==2.18.0
# via rich
rich==13.7.1
# via textual
textual==0.66.0
sniffio==1.3.1
# via anyio
# via httpx
textual==0.74.0
# via textual-serve
typing-extensions==4.12.2
# via anyio
# via rich
# via textual
uc-micro-py==1.0.3
# via linkify-it-py

View File

@@ -16,6 +16,8 @@ aiohttp-jinja2==1.6
# via textual-serve
aiosignal==1.3.1
# via aiohttp
async-timeout==4.0.3
# via aiohttp
attrs==23.2.0
# via aiohttp
frozenlist==1.4.1
@@ -45,9 +47,10 @@ pygments==2.18.0
# via rich
rich==13.7.1
# via textual
textual==0.66.0
textual==0.74.0
# via textual-serve
typing-extensions==4.12.2
# via rich
# via textual
uc-micro-py==1.0.3
# via linkify-it-py

View File

@@ -23,9 +23,9 @@ class AppService:
self,
command: str,
*,
write_bytes: Callable[[bytes], Awaitable],
write_str: Callable[[str], Awaitable],
close: Callable[[], Awaitable],
write_bytes: Callable[[bytes], Awaitable[None]],
write_str: Callable[[str], Awaitable[None]],
close: Callable[[], Awaitable[None]],
debug: bool = False,
) -> None:
self.command = command
@@ -35,7 +35,7 @@ class AppService:
self.debug = debug
self._process: Process | None = None
self._task: asyncio.Task | None = None
self._task: asyncio.Task[None] | None = None
self._stdin: asyncio.StreamWriter | None = None
self._exit_event = asyncio.Event()
@@ -268,7 +268,23 @@ class AppService:
Args:
data: Encoded meta data.
"""
meta_data = json.loads(data)
meta_data: dict[str, object] = json.loads(data)
meta_type = meta_data["type"]
if meta_data["type"] == "exit":
if meta_type == "exit":
await self.remote_close()
elif meta_type == "open_url":
payload = json.dumps(
[
"open_url",
{
"url": meta_data["url"],
"new_tab": meta_data["new_tab"],
},
]
)
await self.remote_write_str(payload)
else:
log.warning(
f"Unknown meta type: {meta_type!r}. You may need to update `textual-serve`."
)

File diff suppressed because one or more lines are too long