-
I am writing an application which provides both, a CLI (using I have tried to create a minimal code example to show how far I have gotten and attached it below: import argparse
from rich.prompt import Confirm, PromptBase
from textual.app import App
from textual.widgets import Input, Static
class TextualPrompt(PromptBase):
tui = None
@classmethod
def get_input(cls, console, prompt, password, stream=None):
inp = Input(value="test")
cls.tui.mount(inp)
inp.focus()
# TODO: how can I await the Input.Submitted event here to return its result?
class AskCommand:
def __init__(self, prompt=Confirm):
self.prompt = prompt
def execute(self):
return self.prompt.ask("Do you love Textualize?")
class PromptApp(App):
BINDINGS = [("a", "ask", "Ask")]
def compose(self):
yield Static("Hello from textual!")
def on_input_submitted(self, event):
"""TODO."""
event.sender.remove()
return event.value
async def action_ask(self):
TextualPrompt.tui = self
ask = AskCommand(TextualPrompt)
res = ask.execute()
if res:
await self.mount(Static("Awesome! Make sure to star our Github repo!"))
else:
await self.mount(Static("Let us know how we can improve!"))
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("interface", choices=["cli", "tui"])
args = parser.parse_args()
if args.interface == "cli":
ask = AskCommand()
print(ask.execute())
elif args.interface == "tui":
PromptApp().run() The Any pointers/ideas/help is welcome 🙂 |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
I think what you would need to do is have an app that accepts the input, then call That way you can |
Beta Was this translation helpful? Give feedback.
-
After quite some tinkering I think I have found a way to make this work. Looks like there are multiple key aspects:
I am posting the code below in case you have some suggestions on how it might be improved further: import argparse
import asyncio
from rich.prompt import Confirm, InvalidResponse
from textual.app import App
from textual.widgets import Input, Static
class TextualPrompt(Confirm):
tui = None
async def __call__(self, *, default=..., stream=None):
while True:
self.pre_prompt()
prompt = self.make_prompt(default)
value = await self.get_input(self.console, prompt, self.password, stream=stream)
if value == "" and default != ...:
return default
try:
return_value = self.process_response(value)
except InvalidResponse as error:
self.on_validate_error(value, error)
continue
else:
return return_value
@classmethod
async def get_input(cls, console, prompt, password, stream=None):
inp = Input()
await cls.tui.mount(inp)
inp.focus()
while cls.tui.is_mounted(inp):
await asyncio.sleep(0)
return inp.value
class AskCommand:
def __init__(self, prompt=Confirm):
self.prompt = prompt
def execute(self):
return self.prompt.ask("Do you love Textualize?")
class PromptApp(App):
BINDINGS = [("a", "ask", "Ask")]
def compose(self):
yield Static("Hello from textual!")
def on_input_submitted(self, event):
event.sender.remove()
def action_ask(self):
asyncio.create_task(self._async_action_ask())
async def _async_action_ask(self):
TextualPrompt.tui = self
ask = AskCommand(TextualPrompt)
res = await ask.execute()
if res:
await self.mount(Static("Awesome! Make sure to star our Github repo!"))
else:
await self.mount(Static("Let us know how we can improve!"))
self.refresh()
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("interface", choices=["cli", "tui"])
args = parser.parse_args()
if args.interface == "cli":
ask = AskCommand()
res = ask.execute()
if res:
print("Awesome! Make sure to star our Github repo!")
else:
print("Let us know how we can improve!")
elif args.interface == "tui":
PromptApp().run() |
Beta Was this translation helpful? Give feedback.
After quite some tinkering I think I have found a way to make this work. Looks like there are multiple key aspects:
Input
widget uponon_input_submitted
App.is_mounted(widget)
to check whether or not my input object has been unmounted during myget_input
methodI am posting the code below in case you have some suggestions on how it might be improved further: