Properties
Live components have properties that store data and can be easily accessed within the component's class and template. Properties are defined as attributes on component classes and can be accessed and modified on both the server and client-side.
This section discusses the basics of adding a property to a component and using it in your application.
Initializing properties
In a standard Python class, to initialize properties or attributes, we use the __init__() dunder method. However, in a PyBlade Live component's class, you can set initial values for properties within your component's mount() method.
from pyblade.live import LiveComponent
from app.models import Task
class TodoList(LiveComponent):
def mount(self):
self.tasks = Task.objects.all()In this example, when the component renders for the first time, all the existing tasks in the database are made available through the tasks variable.
Alternatively, you may declare properties in your component class, making them what are known as "class attributes". For example, let's create a title property in the TodoList component:
from pyblade.live import LiveComponent
class TodoList(LiveComponent):
title = "Learn PyBlade"
def render(self):
return self.render_template()Accessing properties in the template
Component properties are automatically made available to the component's context. You can reference them using standard PyBlade syntax. Here we'll display the value of the title property:
<div>
<h1>Title: {{ title }}</h1>
</div>The rendered output of this component would be:
<div>
<h1>Title: Learn PyBlade</h1>
</div>Sharing additional data with the template
In addition to accessing class properties from the template, you can explicitly pass data to the template from the render() method, like you might typically do from a view in Django. This can be useful when you want to pass additional data without first storing it as a property — because properties have specific security implications.
To pass additional data to the component's template in the render() method, you can pass a dictionary as the context of the returned render_template() call. For example, let's say you want to pass the task status to the template.
from pyblade.live import LiveComponent
class TodoList(LiveComponent):
title = "Learn PyBlade"
def render(self):
return self.render_template(context={"status": "In progress"})
Now you may access the status property from the component's template like this:
<div>
<h1>Title: {{ title }}</h1>
<span>Status: {{ status }}</span>
</div>Data binding
One of PyBlade Live Component's most powerful features is "data binding": the ability to automatically keep properties in-sync with form inputs on the page.
Live components supports two-way data binding through the pb:model HTML attribute. This allows you to easily synchronize data between component properties and HTML inputs, keeping your user interface and component state in sync.
Let's use the pb:model directive to bind the title property in our TodoList component to a basic input element:
from pyblade.live import LiveComponent
class TodoList(LiveComponent):
title: str = ""
def render(self):
return self.render_template()<div>
<h1>Title : {{ title }}</h1>
<form action="">
<input type="text" pb:model="title" /> <!-- [!code highlight] -->
</div>Any changes made to the text input will be automatically synchronized with the title property in your PyBlade Live component.
Why isn't my component live updating as I type?
If you tried this in your browser and are confused why the title isn't automatically updating, it's because PyBlade Live only updates a component when an "action" is submitted — like pressing a submit button — not when a user types into a field. This cuts down on network requests and improves performance.
Pro tip
To enable "live" updating as a user types, you can use pb:model.live instead.
Let's add a method in our component's class so that we can trigger an "action" from the browser to test it out.
from pyblade.live import LiveComponent
from .models import Task
class TodoList(LiveComponent):
title: str = ""
def add_task(self):
if title:
Task.objects.create(title)
def render(self):
return self.render_template()And now we will introduce pb:click which allows you to call and execute a method defined in a PyBlade Live component's class right from the browser.
<div>
<h1>Title : {{ title }}</h1>
<input type="text" pb:model="title" />
<button pb:click="add_task">Add Task</button> <!-- [!code highlight] -->
</div>This time, in the above example, the text input's value will synchronize with the title property on the server when the "Add Task" button is clicked.
This is just scratching the surface of pb:model. We will go deeper when we'll talk about Forms in Live Components.
Accessing properties from JavaScript
In a component's @script, $pb gives you its properties by name:
<div>
<input pb:model="title">
</div>
@script
<script>
console.log($pb.title);
</script>
@endscript$pb.title is what the page holds: after the user has typed into the field, it is what they typed, even before it has been sent. $pb.$get('title') does the same.
Setting properties
Setting a property keeps the new value in the browser, like typing into a pb:model field does. It is sent with the next request the component makes:
$pb.title = 'Draft';To send it now, and render the component again with it, use $set():
$pb.$set('title', 'Draft');Setting a property from JavaScript goes through the same updating() and updated() hooks as a pb:model field.
Watching properties
$watch() calls a function whenever a property changes, with the new value and the old one:
$pb.$watch('status', (now, before) => {
if (now === 'done') confetti();
});It is called when an answer from the server changes the property, and not when an answer leaves it as it was. $watch() returns a function that stops watching.
Security concerns
While PyBlade Live properties are a powerful feature, there are a few security considerations that you should be aware of before using them.