<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Jay Shaffstall</title>
    <description>The latest articles on DEV Community by Jay Shaffstall (@jshaffstall).</description>
    <link>https://dev.to/jshaffstall</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1389617%2Fc056f951-89f6-4a6b-8663-3cfa29772a34.jpeg</url>
      <title>DEV Community: Jay Shaffstall</title>
      <link>https://dev.to/jshaffstall</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/jshaffstall"/>
    <language>en</language>
    <item>
      <title>Introduction to Data Bindings</title>
      <dc:creator>Jay Shaffstall</dc:creator>
      <pubDate>Thu, 23 May 2024 18:51:29 +0000</pubDate>
      <link>https://dev.to/anvil/introduction-to-data-bindings-1op4</link>
      <guid>https://dev.to/anvil/introduction-to-data-bindings-1op4</guid>
      <description>&lt;p&gt;Data Bindings are a useful feature in Anvil that make it easy to keep your UI components in sync with your data. A Data Binding associates a property of a component with a single Python expression, saving you from repeating similar sorts of assignments all over your code. In this tutorial we're going to look at what Data Bindings are, how you can use them, and common gotchas.&lt;/p&gt;

&lt;p&gt;We're going to look at data bindings by developing an example app for logging our favorite movie. Our tutorial will have three main parts:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;We'll build an app and use data bindings to display movie data&lt;/li&gt;
&lt;li&gt;Next, we'll extend the functionality to edit the data&lt;/li&gt;
&lt;li&gt;Then, we'll store the data to a Data Table&lt;/li&gt;
&lt;li&gt;Finally, we'll make some changes so that edits to our data persist when the app is re-run&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The final app will look something like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fx6wc9urncpyb0n73bliq.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fx6wc9urncpyb0n73bliq.png" alt="Final App"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;All of this is done in Anvil's hosted environment through your web browser, all in Python. &lt;/p&gt;

&lt;p&gt;If you're in a hurry, you can click &lt;a href="https://anvil.works/build#clone:T7JHC3NVHTP2UBWX%3dD3UJZJZBEWPJELCQQKPF3TZ4" rel="noopener noreferrer"&gt;here&lt;/a&gt; to clone the app and explore the finished version in Anvil.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;If you're completely new to Anvil, you may want to try a &lt;a href="https://anvil.works/learn/tutorials/feedback-form?utm_source=dev_to_data_binding" rel="noopener noreferrer"&gt;5-minute tutorial&lt;/a&gt; before beginning this tutorial.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Let's get started!&lt;/p&gt;

&lt;h2&gt;
  
  
  What is a Data Binding?
&lt;/h2&gt;

&lt;p&gt;When you want to display the contents of a variable in a visual component you would normally use code like this:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

self.label_1.text = self.title


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;If you wanted to update the Label component when &lt;code&gt;self.title&lt;/code&gt; changed, you would need to repeat that line of code in each place the variable changed. A data binding allows you to establish the relationship between the visual component and the variable once, eliminating all the assignment statements from your code.&lt;/p&gt;

&lt;h2&gt;
  
  
  Chapter 1: Displaying Our Favorite Movie in a Form
&lt;/h2&gt;

&lt;p&gt;We'll create an app that shows information about your favorite movie. We'll use data bindings to display data from a dictionary into Anvil components.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 1: Create an App&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://anvil.works/login" rel="noopener noreferrer"&gt;Log in&lt;/a&gt; to Anvil and click 'Create a new app'. Choose the Material Design 3 theme.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6s4pu8xyu3l85ifte5dp.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6s4pu8xyu3l85ifte5dp.png" alt="Create the App"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;That gives us an app with a &lt;a href="https://anvil.works/docs/ui#forms" rel="noopener noreferrer"&gt;Form &lt;/a&gt;named &lt;code&gt;Form1&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 2: Add components to your form&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;We'll build our UI by dragging-and-dropping &lt;a href="https://anvil.works/docs/ui/components" rel="noopener noreferrer"&gt;components &lt;/a&gt;from the &lt;a href="https://anvil.works/docs/editor#toolbox" rel="noopener noreferrer"&gt;Toolbox &lt;/a&gt;into the page.&lt;/p&gt;

&lt;p&gt;We want to display the movie name, the year it was released, the name of the director, and a summary of the movie's plot.&lt;/p&gt;

&lt;p&gt;Add eight Labels, four for the titles of what we're displaying and four for the values to display. We'll also add a Label into the title slot.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhuip51psi280hjxd8v1z.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhuip51psi280hjxd8v1z.gif" alt="Creating the form"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The final form should look something like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3lzvx5nnpx97b3faspj6.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3lzvx5nnpx97b3faspj6.png" alt="The Final Form"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 3: Initialize data sources&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Every Anvil component and Form has an &lt;code&gt;item&lt;/code&gt; property, meant specifically for storing data of any kind. Our data source will be a dictionary that we'll store in the &lt;code&gt;item&lt;/code&gt; property of our Form.&lt;/p&gt;

&lt;p&gt;Let's go to the Editor's &lt;a href="https://anvil.works/docs/editor#form-editor" rel="noopener noreferrer"&gt;Code view&lt;/a&gt; and create this dictionary in the &lt;code&gt;__init__&lt;/code&gt; function of the form:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

class Form1(Form1Template):
    def __init__(self, **properties):
        self.item = {
            'movie_name': 'Back to the Future',
            'year': 1985,
            'director': 'Robert Zemeckis',
            'summary': 'Doc Brown invents a nuclear powered time machine, which Marty McFly '
                       'then uses to nearly erase himself from existence.'
        }

        # Set Form properties and Data Bindings.
        self.init_components(**properties)


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Note that the dictionary is created before the call to &lt;code&gt;self.init_components&lt;/code&gt;. This is because the data bindings are processed in that call, so our data source (the &lt;code&gt;self.item&lt;/code&gt; dictionary) must be set up before that.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 4: Set up data bindings&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Now we need to tell Anvil what data to associate with which components (e.g. the &lt;em&gt;data bindings&lt;/em&gt;). We'll bind the data from our &lt;code&gt;self.item&lt;/code&gt; dictionary to the &lt;code&gt;text&lt;/code&gt; property of the Labels we just added.&lt;/p&gt;

&lt;p&gt;To set up a data binding, click on the component that you want to have Anvil automatically set. I'll start with the label that will hold the name of the movie.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffic600jnhgxki0ef5u8j.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffic600jnhgxki0ef5u8j.png" alt="Setup Data Binding"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You can find the &lt;code&gt;text&lt;/code&gt; property at the top of the Properties Panel. We could type something in there but that would then always show that text. Instead, click the link icon to the right of the &lt;code&gt;text&lt;/code&gt; property. This will cause the display to change and the link icon to turn blue, indicating that the data binding is active.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fyhg648tf6y3mg8fmp574.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fyhg648tf6y3mg8fmp574.png" alt="Text Property"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Set this data binding to the &lt;code&gt;movie_name&lt;/code&gt; key of our dictionary:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fr69d507q2qo5mkf1akpt.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fr69d507q2qo5mkf1akpt.png" alt="Data Binding"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We'll now do the same for the other labels, using &lt;code&gt;self.item['year']&lt;/code&gt; for the year the movie was released, &lt;code&gt;self.item['director']&lt;/code&gt; for the director's name, and &lt;code&gt;self.item['summary']&lt;/code&gt; for the plot summary.&lt;/p&gt;

&lt;p&gt;After those are set up, we can double check that the data bindings are correct by looking at the Form's Data Binding summary. You can find this by selecting your Form and scrolling to the bottom of the Properties Panel.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fpu923bwp3ohoek8nkoma.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fpu923bwp3ohoek8nkoma.gif" alt="Checking Data Bindings"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 5: Run the app&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Your data bindings are all set! We've told the components which variables to pull data from to populate their text properties.&lt;/p&gt;

&lt;p&gt;Run the app now and see the information about your movie displayed in the Form. We didn't need to use any code to set the &lt;code&gt;text&lt;/code&gt; properties of any of the Labels, they were all set through the data bindings we set up in the Labels' &lt;code&gt;text&lt;/code&gt; properties.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffxmyj9fxwj211zxu3mvq.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffxmyj9fxwj211zxu3mvq.gif" alt="Running the App"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Chapter 2: Edit Movie Data
&lt;/h2&gt;

&lt;p&gt;We'll next allow the user to edit the movie data. For this, we'll use a feature of data bindings called &lt;a href="https://anvil.works/learn/tutorials/data-bindings/docs/client/data-bindings#two-way-data-bindings" rel="noopener noreferrer"&gt;writeback&lt;/a&gt;. You can toggle data binding writeback for user-editable components. This means that when a user changes a property that has a data binding, the original variable will be updated with the new value.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 1: Create your edit form&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Create a second Form in your app by clicking the three dots to the right of Client Code and choose Add Form. Select Blank Panel and rename it to &lt;code&gt;MovieEdit&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fx4whktdia561015k7vrp.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fx4whktdia561015k7vrp.gif" alt="Adding MovieEdit Form"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Start by dropping a ColumnPanel into the page. Setting up the Form is very similar to what we did with our previous Form, but we'll use TextBoxes instead of Labels, since we want the user to be able to edit the fields. Use a TextArea for the plot summary since that will usually be longer than a single line. This is how it should look:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fl7dk56v1jm6ow0qgwpuz.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fl7dk56v1jm6ow0qgwpuz.png" alt="MovieEdit Form"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 2: Set up data bindings&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Setting up data bindings is much the same as before. Here's the one for &lt;code&gt;text_box_1&lt;/code&gt;, binding its text property to &lt;code&gt;self.item['movie_name']&lt;/code&gt;:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fa81eg7yxi73svk2v0llk.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fa81eg7yxi73svk2v0llk.png" alt="Text Data Binding"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In this case, though, notice that there's a W at the right end of the data binding. That &lt;code&gt;W&lt;/code&gt; stands for &lt;a href="https://anvil.works/learn/tutorials/data-bindings/docs/client/data-bindings#two-way-data-bindings" rel="noopener noreferrer"&gt;writeback &lt;/a&gt;and will show on any property that's user-editable. Writeback defaults to inactive, but if you click the W you can activate it.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fdz34n7jo5i6p6b0cqv68.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fdz34n7jo5i6p6b0cqv68.png" alt="Writeback Active"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;When writeback is active and the user edits the value in the Text Box, Anvil will update the original data binding source with those changes. It's the equivalent of you executing &lt;code&gt;self.item['movie_name'] = self.text_box_1.text&lt;/code&gt; when the TextBox value changes.&lt;/p&gt;

&lt;p&gt;Since our purpose is to allow the user to edit the movie information, we want to enable writeback for all of these editable components. Set up the rest, and then check them in the Form's Data Binding summary. They should look like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fj0pdtmmcf0d3ru3b4jiy.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fj0pdtmmcf0d3ru3b4jiy.png" alt="Data Bindings"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Note that we didn't write any code in the &lt;code&gt;MovieEdit&lt;/code&gt; Form. That's the value of data bindings, to automate the boilerplate code of assigning values to visual components and then getting the values out when the user makes changes.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 3: Trigger your edit form&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;We want to access our &lt;code&gt;MovieEdit&lt;/code&gt; Form from our main Form. We'll do this by displaying the &lt;code&gt;MovieEdit&lt;/code&gt; form in an alert every time a user clicks a button.&lt;/p&gt;

&lt;p&gt;Add a Button on &lt;code&gt;Form1&lt;/code&gt;. Set its text to 'Edit movie information' and change its name to edit_button.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fkf9iwpskd1fhj6ndbtri.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fkf9iwpskd1fhj6ndbtri.png" alt="Edit Movie Button"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We want some code to run when this Button is clicked. We'll use &lt;a href="https://anvil.works/docs/ui/components#events" rel="noopener noreferrer"&gt;events &lt;/a&gt;for this. Select the Button and click on 'on &lt;code&gt;click&lt;/code&gt; event' in the Object Palette. This will open the Editor's &lt;a href="https://anvil.works/docs/editor#form-editor" rel="noopener noreferrer"&gt;'Split' view&lt;/a&gt; and automatically create an &lt;code&gt;edit_button_click&lt;/code&gt; method associated with the button.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fyvnm3pat9qx47hoeu15s.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fyvnm3pat9qx47hoeu15s.png" alt="Creating the Click Event"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We'll write the code that will run when we click &lt;code&gt;edit_button&lt;/code&gt; inside this method. First, import the &lt;code&gt;MovieEdit&lt;/code&gt; Form at the top of the Form:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

from ..MovieEdit import MovieEdit


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;And then in the &lt;code&gt;edit_button_click&lt;/code&gt; function itself create an instance of the &lt;code&gt;MovieEdit&lt;/code&gt; Form:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

def edit_button_click(self, **event_args):
    editing_form = MovieEdit(item=self.item)


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;When we create the instance of the &lt;code&gt;MovieEdit&lt;/code&gt; Form, we're going to pass our dictionary of movie information as its &lt;code&gt;item&lt;/code&gt; property. That will make it available in &lt;code&gt;MovieEdit&lt;/code&gt; as &lt;code&gt;self.item&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Now we're going to display the Form in an &lt;a href="https://anvil.works/docs/client/alerts-and-notifications" rel="noopener noreferrer"&gt;alert&lt;/a&gt;. We'll set the content of the alert to be &lt;code&gt;editing_form&lt;/code&gt; and the &lt;code&gt;large&lt;/code&gt; property to be &lt;code&gt;True&lt;/code&gt;:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

def edit_button_click(self, **event_args):
    editing_form = MovieEdit(item=self.item)
    alert(content=editing_form, large=True)


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Run the app and click the editing button to see the movie information in the text boxes and text area. If any of the information doesn't show up in a component, double check the data bindings.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmcdj3yfuw7ahys79ccj5.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmcdj3yfuw7ahys79ccj5.gif" alt="Testing the Alert"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 4: Read the changed values&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The next step is to get the changed values back into the main Form. We are actually almost there since &lt;code&gt;MovieEdit&lt;/code&gt;'s writeback data bindings are already changing &lt;code&gt;Form1&lt;/code&gt;'s &lt;code&gt;self.item&lt;/code&gt; dictionary. But &lt;code&gt;Form1&lt;/code&gt; doesn't know that those values have changed, and so doesn't know to rerun the data bindings to update the components.&lt;/p&gt;

&lt;p&gt;We need to tell Form1 to update its data bindings after the alert returns. We'll do this with &lt;code&gt;self.refresh_data_bindings()&lt;/code&gt;:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

def edit_click(self, **event_args):
        editing_form = MovieEdit(item=self.item)
        alert(content=editing_form, large=True)
        self.refresh_data_bindings()


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Run the app now and edit the movie information to see it changed in &lt;code&gt;Form1&lt;/code&gt;!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fcnwbfqzu5vlv7pku1h2o.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fcnwbfqzu5vlv7pku1h2o.gif" alt="Editing Movie Data"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We can now edit our app's movie data!&lt;/p&gt;

&lt;h2&gt;
  
  
  Chapter 3: Storing data in Data Tables
&lt;/h2&gt;

&lt;p&gt;We can now edit our favorite movie data, but because it's stored in a dictionary, it won't persist when the app is re-run. Because of this, you'll usually want to store data in a database when using data bindings.&lt;/p&gt;

&lt;p&gt;In this chapter, we'll change our app to store the movie data in a &lt;a href="https://anvil.works/docs/data-tables" rel="noopener noreferrer"&gt;Data Table&lt;/a&gt; so that edits to the data will persist between runs of the app. Data Tables are an out-of-the-box option for data storage in Anvil, and they're backed by PostgreSQL.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 1: Create a Data Table&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In the Sidebar Menu, choose Data to open up your app's Data Tables.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fcymgvu3u6gn3ois34vda.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fcymgvu3u6gn3ois34vda.png" alt="Data Tables"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Next, click '+ Add Table' to add a new Data Table to the app. Name the table 'movies'.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fkzt12bfzov2px0vea5e5.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fkzt12bfzov2px0vea5e5.gif" alt="Add the Table"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;With the new table selected, you'll see the interface for editing the Data Table. We'll set up our Data Table with 4 columns. We need to add the following columns:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;movie_name&lt;/code&gt; (a text column)&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;year&lt;/code&gt; (a number column)&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;director&lt;/code&gt; (a text column)&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;summary&lt;/code&gt; (a text column).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;We can now copy the data from the &lt;code&gt;self.item&lt;/code&gt; dictionary and add it to our table.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ft4y1pbltieod079cf7tm.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ft4y1pbltieod079cf7tm.png" alt="Filled Data Table"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Notice that we've used the same names for the columns as we used for the &lt;code&gt;self.item&lt;/code&gt; keys, so that we do not need to change our data bindings.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 2: Configure the Data Table&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Data tables live on the server, which is a secure and trusted environment. Client Forms and client code run in the user's web browser, which the user has access to. Sufficiently motivated and knowledgeable users can modify the client code of any web app to make it do anything they'd like it to do, including removing any security checks you might put into client code.&lt;/p&gt;

&lt;p&gt;By default Anvil Data Tables are &lt;a href="https://anvil.works/docs/data-tables/data-security#permissions" rel="noopener noreferrer"&gt;only accessible from server code&lt;/a&gt; because this is the most secure option. You can see that in the Data Table interface:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fosn5c4s73xthv50s8y5p.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fosn5c4s73xthv50s8y5p.png" alt="Data Table Security"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You have two other options for client code. You can give client code read access (client code can search this table) or full access (client code can search, edit, and delete). Read access is appropriate for tables where there are no access restrictions needed on who can see data in the table. Full access is almost never appropriate for client code since it allows sufficiently motivated users to bypass all your security restrictions and all your validation code.&lt;/p&gt;

&lt;p&gt;We're going to use read access for our table since we only have the one row that we want all the users to be able to read. After making that change the configuration should look like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ftchqzkdt0y0xrdy1scvp.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ftchqzkdt0y0xrdy1scvp.png" alt="Data Table Client Reading"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 3: Read from the Data Table&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Now we need to change &lt;code&gt;Form1&lt;/code&gt; to use the &lt;code&gt;movies&lt;/code&gt; Data Table as the source for &lt;code&gt;self.item&lt;/code&gt; instead of our dictionary.&lt;/p&gt;

&lt;p&gt;We can access any Data Table with &lt;code&gt;app_tables.&amp;lt;data-table-name&amp;gt;&lt;/code&gt;. Since our table only has one row, we want to get the first row from the Data Table into &lt;code&gt;self.item&lt;/code&gt; in the &lt;code&gt;__init__&lt;/code&gt; function for &lt;code&gt;Form1&lt;/code&gt;.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

class Form1(Form1Template):
    def __init__(self, **properties):
        self.item = app_tables.movies.search()[0]

        # Set Form properties and Data Bindings.
        self.init_components(**properties)


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;What &lt;code&gt;app_tables.movies.search()&lt;/code&gt; returns looks a lot like a Python list, so we can use standard list indexing to pull the first item out of the list. What's stored in the list are data table rows, which look enough like Python dictionaries to be used in &lt;code&gt;self.item&lt;/code&gt; with data bindings.&lt;/p&gt;

&lt;p&gt;You should now be able to run the app and see the information from the data table showing on the main page of your app. Editing doesn't yet work, but that's our next step to fix.&lt;/p&gt;

&lt;h2&gt;
  
  
  Chapter 4: Store edits to the Data Table
&lt;/h2&gt;

&lt;p&gt;Because we are not allowing the client to write to our Data Table, we cannot use writeback as we have been.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 1: Update the Data Table&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Instead, we'll use a &lt;a href="https://anvil.works/docs/server" rel="noopener noreferrer"&gt;server function&lt;/a&gt; to update the Data Table.&lt;/p&gt;

&lt;p&gt;Anvil's &lt;a href="https://anvil.works/docs/server" rel="noopener noreferrer"&gt;Server Modules&lt;/a&gt; are a full server-side Python environment. Unlike client code, Server Modules cannot be edited or seen by the user, so we can &lt;a href="https://anvil.works/docs/security#client-vs-server-code" rel="noopener noreferrer"&gt;trust &lt;/a&gt;them to do what we tell them. This is why we'll use a Server Module to write to the Data Table.&lt;/p&gt;

&lt;p&gt;For more information about the difference between client and server code, check out &lt;a href="https://anvil.works/articles/client-vs-server" rel="noopener noreferrer"&gt;this explainer&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Let's create a Server Module. In the App Browser, click the blue '+ Add Server Module' button underneath 'Server Code'. This will open up a code editor.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fal1u4m3pcbtra4ws0kz3.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fal1u4m3pcbtra4ws0kz3.png" alt="Creating Server Module"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Our server function will be taking in a dictionary from the client and updating the Data Table with its contents. We'll decorate it with &lt;code&gt;@anvil.server.callable&lt;/code&gt; so that we can access it from client code. Remember that we're still dealing with just the single row in the Data Table.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

@anvil.server.callable
def update_movie(movie_data):
    row = app_tables.movies.search()[0]
    row['director'] = movie_data['director']
    row['movie_name'] = movie_data['movie_name']
    row['summary'] = movie_data['summary']
    row['year'] = movie_data['year']


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Server functions also give us a place to put any data validation we need. In the case of the movie data, we probably don't want to allow any of the fields to be empty.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

@anvil.server.callable
def update_movie(movie_data):
    if movie_data['director'] and movie_data['movie_name'] and movie_data['summary'] and movie_data['year']:
        row = app_tables.movies.search()[0]
        row['director'] = movie_data['director']
        row['movie_name'] = movie_data['movie_name']
        row['summary'] = movie_data['summary']
        row['year'] = movie_data['year']


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;The exact validation you do depends on your app and the server function, but it's the spot to do last minute validation before you write changes to the Data Table.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 2: Make a copy of the Data Table row&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Switch to the Code view for &lt;code&gt;Form1&lt;/code&gt; and look at the &lt;code&gt;edit_click&lt;/code&gt; function.&lt;/p&gt;

&lt;p&gt;We can no longer pass &lt;code&gt;self.item&lt;/code&gt; directly to the &lt;code&gt;MovieEdit&lt;/code&gt; form, since &lt;code&gt;self.item&lt;/code&gt; is a read-only Data Table row and writeback will not work with it. What we can do instead is convert the item to a dictionary, effectively making a copy of it. Modify the &lt;code&gt;edit_click&lt;/code&gt; function to do so.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

def edit_click(self, **event_args):
        item = dict(self.item)
        editing_form = MovieEdit(item=item)
        alert(content=editing_form, large=True)


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Writeback data bindings will work fine on the dictionary, since it's no longer a Data Table row, but a dictionary with copies of the values that were in the Data Table row.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 3: Update the Data Table row&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;After the alert is closed, we must call the server function to update the Data Table row and pass it the &lt;code&gt;item&lt;/code&gt; dictionary. We do that by using &lt;code&gt;anvil.server.call&lt;/code&gt; to call our &lt;code&gt;update_movie&lt;/code&gt; server function.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

def edit_click(self, **event_args):
        item = dict(self.item)
        editing_form = MovieEdit(item=item)
        alert(content=editing_form, large=True)
        anvil.server.call('update_movie', item)


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Step 4: Update &lt;code&gt;self.item&lt;/code&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Now that the server function has been called, we must update the main Form's data. We'll reset &lt;code&gt;self.item&lt;/code&gt; by getting the now updated row from our Data Table, then we'll use &lt;code&gt;self.refresh_data_bindings()&lt;/code&gt; to trigger the Form to update the text property of its components:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

def edit_click(self, **event_args):
    item = dict(self.item)
    editing_form = MovieEdit(item=item)
    alert(content=editing_form, large=True)
    anvil.server.call('update_movie', item)
    self.item = app_tables.movies.search()[0]
    self.refresh_data_bindings()


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Now, edits to the app will persist when you re-run the app. All users will see the edits that any users make.&lt;/p&gt;

&lt;p&gt;Run the app, edit the movie information, and then stop the app and verify the data in the Data Tables has changed.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fr6hugk77funldkrnk54k.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fr6hugk77funldkrnk54k.gif" alt="Data Tables Changing"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;And that's it! We have built a simple app to showcase data for your favorite movie. From here, you could extend your app's functionality, for example by allowing users to have their own separate favorite movies.&lt;/p&gt;

&lt;h2&gt;
  
  
  What's Next?
&lt;/h2&gt;

&lt;p&gt;Head to the &lt;a href="https://anvil.works/learn/tutorials?utm_source=dev_to_data_binding" rel="noopener noreferrer"&gt;Anvil Learning Centre&lt;/a&gt; for more guided tutorials, or head to our &lt;a href="https://anvil.works/learn/examples?utm_source=dev_to_data_binding" rel="noopener noreferrer"&gt;examples &lt;/a&gt;page to see the variety of apps you can build with Anvil.&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
