Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

command01.py example on how to use Provider freezes #3934

Closed
josevnz opened this issue Dec 28, 2023 · 3 comments
Closed

command01.py example on how to use Provider freezes #3934

josevnz opened this issue Dec 28, 2023 · 3 comments

Comments

@josevnz
Copy link

josevnz commented Dec 28, 2023

Hello,

The example app showcased on 'command01.py' freezes and displays nothing but an blank screen. Ctrl+ Doesn't do anything.

This is how I call the script. One one terminal:

python3 -m venv ~/virtualenv/Textualize/
. ~/virtualenv/Textualize/bin/activate
pip install textual~=0.46.0
pip install textual-dev==1.3.0
textual diagnose

And on a different terminal, same machine:

. ~/virtualenv/Textualize/bin/activate
textual run --dev  $PWD/command01.py

The script

I made some modifications to print out where it was getting stuck, but it is mostly the same script as shown here:

#!/usr/bin/env python3
from __future__ import annotations

from functools import partial
from pathlib import Path
from typing import Any

from rich.style import Style
from rich.syntax import Syntax
from textual.app import App, ComposeResult
from textual.command import Hit, Hits, Provider
from textual.containers import VerticalScroll
from textual.screen import Screen
from textual.widgets import Static


class PythonFileCommands(Provider):
    """A command provider to open a Python file in the current working directory."""

    def __init__(self, screen: Screen[Any], match_style: Style | None = None):
        super().__init__(screen, match_style)
        self.python_paths = None
        self.app.log(f"Done with Provider init")

    def read_files(self) -> list[Path]:
        """Get a list of Python files in the current working directory."""
        files = list(Path("../").glob("*.py"))
        self.app.log.info(f"Got files: {files}")
        return files

    async def startup(self) -> None:
        """Called once when the command palette is opened, prior to searching."""
        worker = self.app.run_worker(self.read_files, thread=True)
        self.python_paths = await worker.wait()
        self.app.log.info(f"Done startup, paths = {self.python_paths}")

    async def search(self, query: str) -> Hits:
        """Search for Python files."""
        matcher = self.matcher(query)

        my_app = self.app
        assert isinstance(my_app, ViewerApp)

        for path in self.python_paths:
            command = f"open {str(path)}"
            score = matcher.match(command)
            if score > 0:
                yield Hit(
                    score,
                    matcher.highlight(command),
                    partial(my_app.open_file, path),
                    help="Open this file in the viewer",
                )


class ViewerApp(App):
    """Demonstrate a command source."""

    COMMANDS = App.COMMANDS | {PythonFileCommands}

    def compose(self) -> ComposeResult:
        with VerticalScroll():
            yield Static(id="code", expand=True)
        self.log.info("Done with compose")

    def open_file(self, path: Path) -> None:
        """Open and display a file with syntax highlighting."""

        syntax = Syntax.from_path(
            str(path),
            line_numbers=True,
            word_wrap=False,
            indent_guides=True,
            theme="github-dark",
        )
        self.query_one("#code", Static).update(syntax)
        self.log.info(f"Done reading file {path}")


if __name__ == "__main__":
    app = ViewerApp()
    app.run()

Environment

Here is my environment information

(Textualize) [josevnz@dmaf5 Textualize]$ textual diagnose

Textual Diagnostics

Versions

Name Value
Textual 0.46.0
Rich 13.6.0

Python

Name Value
Version 3.11.6
Implementation CPython
Compiler GCC 12.3.1 20230508 (Red Hat 12.3.1-1)
Executable /home/josevnz/virtualenv/Textualize/bin/python

Operating System

Name Value
System Linux
Release 6.5.12-100.fc37.x86_64
Version #1 SMP PREEMPT_DYNAMIC Mon Nov 20 22:28:44 UTC 2023

Terminal

Name Value
Terminal Application GNOME Terminal
TERM xterm-256color
COLORTERM truecolor
FORCE_COLOR Not set
NO_COLOR Not set

Rich Console options

Name Value
size width=262, height=42
legacy_windows False
min_width 1
max_width 262
is_terminal True
encoding utf-8
max_height 42
justify None
overflow None
no_wrap False
highlight None
markup None
height None

Output of 'textual console`

▌Textual Development Console v0.46.0                                                                                                                                                      
▌Run a Textual app with textual run --dev my_app.py to connect.                                                                                                                           
▌Press Ctrl+C to quit.                                                                                                                                                                    
─────────────────────────────────────────────────────────────────────────────── Client '127.0.0.1' connected ────────────────────────────────────────────────────────────────────────────────
[13:26:00] SYSTEM                                                                                                                                                                 app.py:2188
Connected to devtools ( ws://127.0.0.1:8081 )
[13:26:00] SYSTEM                                                                                                                                                                 app.py:2192
---
[13:26:00] SYSTEM                                                                                                                                                                 app.py:2194
driver=<class 'textual.drivers.linux_driver.LinuxDriver'>
[13:26:00] SYSTEM                                                                                                                                                                 app.py:2195
loop=<_UnixSelectorEventLoop running=True closed=False debug=False>
[13:26:00] SYSTEM                                                                                                                                                                 app.py:2196
features=frozenset({'debug', 'devtools'})
[13:26:00] SYSTEM                                                                                                                                                                 app.py:2228
STARTED FileMonitor(set())
[13:26:00] EVENT                                                                                                                                                          message_pump.py:706
Load() >>> ViewerApp(title='ViewerApp', classes={'-dark-mode'}) method=None
[13:26:00] INFO                                                                                                                                                               command01.py:64
Done with compose
[13:26:00] EVENT                                                                                                                                                          message_pump.py:697
Mount() >>> Static(id='code') method=<Widget.on_mount>
[13:26:00] EVENT                                                                                                                                                          message_pump.py:697
Mount() >>> ToastRack(id='textual-toastrack') method=<Widget.on_mount>
[13:26:00] EVENT                                                                                                                                                          message_pump.py:697
Mount() >>> Tooltip(id='textual-tooltip') method=<Widget.on_mount>
[13:26:00] EVENT                                                                                                                                                          message_pump.py:697
Mount() >>> VerticalScroll() method=<Widget.on_mount>
[13:26:00] EVENT                                                                                                                                                          message_pump.py:706
Mount() >>> ViewerApp(title='ViewerApp', classes={'-dark-mode'}) method=None
[13:26:00] SYSTEM                                                                                                                                                                 app.py:2319
ready in 135 milliseconds
[13:26:00] EVENT                                                                                                                                                          message_pump.py:697
Resize(size=Size(width=189, height=33), virtual_size=Size(width=189, height=33)) >>> ViewerApp(title='ViewerApp', classes={'-dark-mode'}) method=<App.on_resize>
[13:26:00] EVENT                                                                                                                                                          message_pump.py:697
Mount() >>> Screen(id='_default') method=<Widget.on_mount>
[13:26:00] EVENT                                                                                                                                                          message_pump.py:697
ScreenResume() >>> Screen(id='_default') method=<Screen.on_screen_resume>
[13:26:00] DEBUG                                                                                                                                                                screen.py:596
VerticalScroll() was focused
[13:26:00] EVENT                                                                                                                                                          message_pump.py:697
Resize(size=Size(width=189, height=33), virtual_size=Size(width=189, height=33)) >>> Screen(id='_default') method=<Screen.on_resize>
[13:26:00] EVENT                                                                                                                                                          message_pump.py:697
Resize(size=Size(width=189, height=33), virtual_size=Size(width=189, height=33)) >>> Screen(id='_default') method=<Screen.on_resize>
[13:26:00] EVENT                                                                                                                                                          message_pump.py:706
Show() >>> Screen(id='_default') method=None
[13:26:00] EVENT                                                                                                                                                          message_pump.py:706
Resize(size=Size(width=189, height=1), virtual_size=Size(width=189, height=1)) >>> Static(id='code') method=None
[13:26:00] EVENT                                                                                                                                                          message_pump.py:706
Show() >>> Static(id='code') method=None
[13:26:00] EVENT                                                                                                                                                          message_pump.py:706
Resize(size=Size(width=189, height=33), virtual_size=Size(width=189, height=33)) >>> VerticalScroll() method=None
[13:26:00] EVENT                                                                                                                                                          message_pump.py:706
Show() >>> VerticalScroll() method=None
[13:26:00] EVENT                                                                                                                                                          message_pump.py:697
Focus() >>> VerticalScroll() method=<Widget.on_focus>
[13:26:00] EVENT                                                                                                                                                          message_pump.py:706
Ready() >>> ViewerApp(title='ViewerApp', classes={'-dark-mode'}) method=None
[13:26:00] EVENT                                                                                                                                                          message_pump.py:697
TerminalSupportsSynchronizedOutput() >>> ViewerApp(title='ViewerApp', classes={'-dark-mode'}) method=<App.on_terminal_supports_synchronized_output>
[13:26:00] SYSTEM                                                                                                                                                                 app.py:3158
SynchronizedOutput mode is supported
─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
[13:26:58] EVENT                                                                                                                                                          message_pump.py:697
Click(x=36, y=4, button=1) >>> VerticalScroll() method=<Widget.on_click>
[13:26:58] EVENT                                                                                                                                                          message_pump.py:697
Click(x=36, y=4, button=1) >>> Screen(id='_default') method=<Widget.on_click>
[13:26:58] EVENT                                                                                                                                                          message_pump.py:706
Click(x=36, y=4, button=1) >>> ViewerApp(title='ViewerApp', classes={'-dark-mode'}) method=None
─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
[13:26:59] EVENT                                                                                                                                                          message_pump.py:697
Key(key='enter', character='\r', name='enter', is_printable=False, aliases=['enter', 'ctrl+m']) >>> VerticalScroll() method=<Widget.on_key>
[13:26:59] EVENT                                                                                                                                                          message_pump.py:697
Key(key='enter', character='\r', name='enter', is_printable=False, aliases=['enter', 'ctrl+m']) >>> Screen(id='_default') method=<Widget.on_key>
[13:26:59] EVENT                                                                                                                                                          message_pump.py:697
Key(key='enter', character='\r', name='enter', is_printable=False, aliases=['enter', 'ctrl+m']) >>> ViewerApp(title='ViewerApp', classes={'-dark-mode'}) method=<App.on_key>
[13:26:59] SYSTEM                                                                                                                                                                 app.py:2801
<action> namespace=ViewerApp(title='ViewerApp', classes={'-dark-mode'}) action_name='quit' params=()
[13:26:59] EVENT                                                                                                                                                          message_pump.py:697
Unmount() >>> Static(id='code') method=<Widget.on_unmount>
[13:26:59] EVENT                                                                                                                                                          message_pump.py:697
Unmount() >>> VerticalScroll() method=<Widget.on_unmount>
[13:26:59] EVENT                                                                                                                                                          message_pump.py:697
Unmount() >>> ToastRack(id='textual-toastrack') method=<Widget.on_unmount>
[13:26:59] EVENT                                                                                                                                                          message_pump.py:697
Unmount() >>> Tooltip(id='textual-tooltip') method=<Widget.on_unmount>


────────────────────────────────────────────────────────────────────────────── Client '127.0.0.1' disconnected ──────────────────────────────────────────────────────────────────────────────
[13:26:59] DEBUG                                                                                                                                                                screen.py:574
focus was removed
[13:26:59] EVENT                                                                                                                                                          message_pump.py:697
Unmount() >>> Screen(id='_default') method=<Widget.on_unmount>
[13:26:59] EVENT                                                                                                                                                          message_pump.py:706
Unmount() >>> ViewerApp(title='ViewerApp', classes={'-dark-mode'}) method=None
Copy link

Thank you for your issue. Give us a little time to review it.

PS. You might want to check the FAQ if you haven't done so already.

This is an automated reply, generated by FAQtory

@josevnz
Copy link
Author

josevnz commented Dec 28, 2023

User error. I was using the wrong keys to make the command pallete to show up.

How to troubleshoot?

  1. Run 'textual keys' and make sure you are pressing the right key combination. Ctrl + \, it will show up on the screen
  2. Call the demo and then you will see the command pallete show up after you press 'Ctrl + '. It won't show up by default (why it should, it is called on demand).

No bug at all, closing this.

@josevnz josevnz closed this as completed Dec 28, 2023
Copy link

Don't forget to star the repository!

Follow @textualizeio for Textual updates.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant