actions docs

This commit is contained in:
Will McGugan
2022-09-29 17:19:07 +01:00
parent 5f97bbd33f
commit 7bfe178c69
3 changed files with 71 additions and 20 deletions

View File

@@ -1,22 +1,26 @@
# Actions
Actions are white-listed functions with a string syntax you can embed in to links and bind to keys. In this chapter wee will discuss how to create actions and how to run them.
Actions are allow-listed functions with a string syntax you can embed in links and bind to keys. In this chapter we will discuss how to create actions and how to run them.
## Action methods
Action methods are methods on your app or widgets prefixed with `action_`. Aside from the prefix these are regular methods which you could call directly if you wished.
!!! information
Action methods may be coroutines (prefixed with `async`).
Let's write an app with a simple action.
```python title="actions01.py" hl_lines="6-8"
--8<-- "docs/examples/guide/actions/actions01.py"
```
The `action_set_background` method is an action which sets the background of the screen. The key handler calls this action if you press the ++r++ key, to set the background color to red.
The `action_set_background` method is an action which sets the background of the screen. The key handler calls this action if you press the ++r++ key to set the background color to red.
Although it is possible (and occasionally useful) to call action methods in this way, they are intended to be parsed from an _action string_. For instance, the string "set_background('red')" is an action string that would call `self.action_set_background('red')`.
Although it is possible (and occasionally useful) to call action methods in this way, they are intended to be parsed from an _action string_. For instance, the string `"set_background('red')"` is an action string that would call `self.action_set_background('red')`.
The following example replaces the immediate call with a call to [action()][textual.widgets.Widget.action] which parses an action strings and dispatches it to the appropriate action method.
The following example replaces the immediate call with a call to [action()][textual.widgets.Widget.action] which parses an action string and dispatches it to the appropriate method.
```python title="actions02.py" hl_lines="10-12"
--8<-- "docs/examples/guide/actions/actions02.py"
@@ -24,33 +28,33 @@ The following example replaces the immediate call with a call to [action()][text
Note that the `action()` method is a coroutine so `on_key` needs to be prefixed with the `async` key.
You will not typically need this in a real app as Textual will run actions for you from key bindings or links. Before we discuss more practical uses for action strings, let's have a look at the syntax for actions.
You will not typically need this in a real app as Textual will run actions in links or key bindings. Before we discuss these, let's have a look at the syntax for action strings.
## Action syntax
## Syntax
Action strings have a simple syntax, which for the most part replicates Python's function call syntax.
!!! important
As much as they look like Python code, Textual does **not** call Python's `eval` function or similar to execute action strings, as this would create a security risk.
As much as they look like Python code, Textual does **not** call Python's `eval` function or similar to compile action strings.
Action strings have the following format:
- The name of an action on is own will call the action method with no parameters. For example, `"bell"` will call `action_bell()`.
- Actions may be followed by braces containing Python objects. For example, the action string `set_background('red')` will call `action_set_background("red")`.
- The name of an action on is own will call the action method with no parameters. For example, an action string of `"bell"` will call `action_bell()`.
- Actions may be followed by braces containing Python objects. For example, the action string `set_background("red")` will call `action_set_background("red")`.
- Actions may be prefixed with a _namespace_ (see below) follow by a dot.
<div class="excalidraw">
--8<-- "docs/images/actions/format.excalidraw.svg"
</div>
### Action parameters
### Parameters
If the action strings contains parameters, these must be valid Python literals. Which means you can include numbers, strings, dicts, lists etc. but you can't include variables or references to any other python symbols.
Consequently `"set_background('blue')"` is a valid action string, but `"set_background(new_color)"` is not &mdash; because `new_color` is a variable and not a literal.
## Actions in links
## Links
Actions may be embedded in links with console markup, which you can introduce the `@click` tag.
@@ -69,7 +73,7 @@ The following example mounts simple static text with embedded action links.
When you click any of the links, Textual runs the `"set_background"` action to change the background to the given color.
## Actions in binding
## Bindings
Textual will also run actions that are bound to keys. The following example adds key [bindings](./input.md#bindings) for the ++r++, ++g++, and ++b++ keys which call the `"set_background"` action.
@@ -86,9 +90,9 @@ Textual will also run actions that are bound to keys. The following example adds
If you run this example, you can change the background by pressing keys in addition to clicking links.
## Action namespaces
## Namespaces
Textual will look for action methods on the widget or app where they are used. If we were to create a [custom widget](./widgets.md#custom-widgets) with an action method, it can have its own set of actions.
Textual will look for action methods on the widget or app where they are used. If we were to create a [custom widget](./widgets.md#custom-widgets) it can have its own set of actions.
The following example defines a custom widget with its own `set_background` action.
@@ -104,4 +108,40 @@ The following example defines a custom widget with its own `set_background` acti
--8<-- "docs/examples/guide/actions/actions05.css"
```
If you click on the links, it will call the action method for the _widget_ where the click is handler. The ++r++, ++g++, and ++b++ keys are defined on the App so will set the background for the app.
There are two instances of the custom widget mounted. If you click the links in either of them it will changed the background for that widget only. The ++r++, ++g++, and ++b++ key bindings are set on the App so will set the background for the screen.
You can optionally prefix an action with a _namespace_, which tells Textual to run actions for a different object.
Textual supports the following action namespaces:
- `app` invokes actions on the App.
- `screen` invokes actions on the screen.
In the previous example if you wanted a link to set the background on the app rather than the widget, we could set a link to `app.set_background('red')`.
## Builtin actions
Textual supports the following builtin actions which are defined on the app.
### Bell
::: textual.app.App.action_bell
options:
show_root_heading: false
### Screenshot
::: textual.app.App.action_screenshot
### Toggle_dark
::: textual.app.App.action_toggle_dark
### Quit
::: textual.app.App.action_quit
*TODO:* document more actions