pythonpython-3.xcommand-line-interfacechatprompt-toolkit

Add a popup dailog in fullscreen app in python prompt_toolkit


I am creating a terminal chat application, where I have a UI with prompt_toolkit. enter image description here There In message box i added some commands to perform some action. To create the Dashboard I used prompt_toolkit.Application And apply my functionalaty on top of this.

class Dashboard(Application):
    """Implemented Dashboard"""

    def __init__(self):
        super().__init__(full_screen=True)
        self.key_bindings = KeyBindings()    
        self.__layout = None
        self.create_layout()
        self.set_layout()
        self.set_key_bind()

    def create_layout(self):
        """Implemented Dashboard.create_layout"""
        self.__layout = VSplit(
            [
                HSplit(
                    [self.__screen_area, self.__message_box]
                ),
                self.__user_section
            ], padding=1, width=2)

    def set_layout(self):
        """Setting the dashboard layout"""
        self.layout = Layout(self.__layout)

    def process_message(self):
        """Implemented send message method"""
        buffer = self.__message_box.buffer
        if buffer:
            if '/' in buffer[0]:
                # INFO: Clear the message box
                self.__message_box.clear()
                buffer = buffer[1:]
                # INFO: Perform the operation
                if buffer in ['clear', 'cls', 'c']:
                    self.__screen_area.clear()
                elif buffer in ['exit', 'quit', 'q']:
                    # add confirm dailog here
                    self.exit()
            else:
                message = self.__message_box.message
                self.__screen_area.send(message)

I want to have a pop for confirm dailog like this enter image description here

And was provided in prompt_toolkit docs

I was trying to add that dailog in py app, but everytime it says Exception This event loop is already running

problem seems like My Dashboard is a loop and i can't have another loop inside the existing one. I am stuck in this point. Any help or suggestion will be healpfull

Git url to my REPO


Solution

  • I haven't exactly fount the solution, but I have found another python package textual. Using texual you can Build sophisticated TUI with a simple Python API.

    Link for GITHUB & PyPI