|
| 1 | +## Management Commands |
| 2 | + |
| 3 | +## `createlivecomponent` |
| 4 | + |
| 5 | +### Description |
| 6 | + |
| 7 | +The `createlivecomponent` command is used to quickly create a new livecomponent. This command generates the necessary Python and HTML files for the component. |
| 8 | + |
| 9 | +### Arguments |
| 10 | + |
| 11 | +#### Positional Arguments |
| 12 | + |
| 13 | +- `app_name` (str): The name of the app. E.g., 'counters'. |
| 14 | +- `component_name` (str): Component name in snake case. The name can use directory separator for creating namespaces. Normally, the first namespace is the app name. E.g., 'counters/click_counter'. |
| 15 | + |
| 16 | +#### Optional Arguments |
| 17 | + |
| 18 | +- `--class-name` (str): Optional class name for the component. E.g., 'ClickCounter'. If not given, the component name, converted to PascalCase, is used. |
| 19 | +- `-f`, `--force` (bool): Overwrite existing files. Default is `False`. |
| 20 | +- `--stateless` (bool): Create a stateless component. Default is `False`. |
| 21 | +- `--minimal` (bool): Create a minimal component without any commands. Default is `False`. |
| 22 | +- `--base-class` (str): Base class for the component. If not given, value from settings is used. |
| 23 | + |
| 24 | + |
| 25 | + |
| 26 | +### Usage Examples |
| 27 | + |
| 28 | +#### Basic Usage |
| 29 | + |
| 30 | +```sh |
| 31 | +python manage.py createlivecomponent counters counters/click_counter |
| 32 | +``` |
| 33 | + |
| 34 | +This command will create the files: |
| 35 | + |
| 36 | +- `counters/components/counters/click_counter/click_counter.py` (Python file with the component class) |
| 37 | +- `counters/components/counters/click_counter/click_counter.html` (HTML template for the component) |
| 38 | + |
| 39 | +#### Create Minimal Statless Component |
| 40 | + |
| 41 | +```sh |
| 42 | +python manage.py createlivecomponent counters counters/click_counter --stateless --minimal |
| 43 | +``` |
| 44 | + |
| 45 | +### Settings Options |
| 46 | + |
| 47 | +#### Base Class Name |
| 48 | + |
| 49 | +The base class name for the component can be configured in the Django settings under the `LIVECOMPONENTS` configuration. Example: |
| 50 | + |
| 51 | +```python |
| 52 | +# myproject/utils.py |
| 53 | +import abc |
| 54 | +from typing import Generic |
| 55 | +from livecomponents import LiveComponent, StatelessLiveComponent |
| 56 | +from livecomponents.types import State |
| 57 | + |
| 58 | + |
| 59 | +class MyLiveComponent(LiveComponent, abc.ABC, Generic[State]): |
| 60 | + class Media: |
| 61 | + js = ["myproject/livecomponent.js"] |
| 62 | + |
| 63 | + |
| 64 | +class MyStatelessLiveComponent(StatelessLiveComponent): |
| 65 | + class Media: |
| 66 | + js = ["myproject/livecomponent.js"] |
| 67 | +``` |
| 68 | + |
| 69 | +```python |
| 70 | +# settings.py |
| 71 | +LIVECOMPONENTS = { |
| 72 | + # ... |
| 73 | + # Settings for the "./manage.py createlivecomponent" command. |
| 74 | + "createlivecomponent": { |
| 75 | + "base_class": "myproject.utils.MyLiveComponent", |
| 76 | + "stateless_base_class": "myproject.utils.MyStatelessLiveComponent", |
| 77 | + }, |
| 78 | +} |
| 79 | +``` |
0 commit comments