← Data apps

Build around a simulation

An Embed shows a ready-made pendulum simulation, made as one web page. The page sends its period and length; the app sends it gravity. Students build the app around it with events: show the period when the page sends it, change gravity with planet buttons, and record readings in a table to answer a science question. The simulation is a component with inputs and outputs, the way real apps use parts other people made.

Someone has already made a pendulum simulation. Your job is the app around it: buttons that change it, and a table that records what it does.

An Embed shows a web page inside your app. Here the page is a pendulum simulation, made as one web file. You don't change the page: you build the app around it.

The page and your events talk by messages, each a name and one value:

The names are listed in the Embed's Properties, under Sends and Listens for. The page only ever gets values, never your events, and your app only gets values from it.

How this lesson works: See it, Build, Check, Make it yours
See itTry the finished thing first, so you know what you are building.

Try the finished app

This is the app you'll build, open on the Run tab. Drag Length in the simulation and watch Period on the right change. Click Moon, then Record reading: a row goes into the table.

Try it yourselfYour edits are saved in this browser.

BuildWatch the short clip, then do the steps. Your app carries on from the step before.

Build 1: show the period

  1. Add an event: Sim sends period. It's in Sim's group, with period already chosen.
  2. Its action: Set PeriodText text to "Period: " & Sim.period & " s"
  3. Run it and drag Length.
When the simulation sends a value · Sound on for the voiceover
Try it yourselfYour edits are saved in this browser.

When Sim sends period, PeriodText shows the period.

    Run your program. This ticks when your code does all of the above.

    BuildWatch the short clip, then do the steps. Your app carries on from the step before.

    Build 2: change gravity

    The three planet buttons are copies of one object, Planet, and each copy has its own g (look at its Properties).

    1. Add an event: Planet is clicked.
    2. Set variable Gravity to Planet.g
    3. Send gravity = Gravity to Sim (in Sim's group).
    4. Set GravityText text to "Gravity: " & Gravity & " m/s²"

    Run it. Does the pendulum swing faster or slower on the Moon?

    Try it yourselfYour edits are saved in this browser.

    Clicking a planet sets Gravity to its g, sends gravity to Sim and shows it in GravityText.

      Run your program. This ticks when your code does all of the above.

      BuildWatch the short clip, then do the steps. Your app carries on from the step before.

      Build 3: record a reading

      Add an event: Record is clicked → Add a row to table Readings, with length Sim.length, gravity Gravity and period Sim.period.

      Try it yourselfYour edits are saved in this browser.

      Record adds a row to Readings with the length, gravity and period.

        Run your program. This ticks when your code does all of the above.

        CheckA quick check that the idea has stuck.

        Check: what gets through?

        The pendulum page also sends a message called angle. Sim's Sends box says only period, length. What happens when angle arrives?

        See itTry the finished thing first, so you know what you are building.

        Use your lab

        Record two lengths on Earth, then the same two lengths on the Moon. Look at your table:

        Try it yourselfYour edits are saved in this browser.

        Make it yoursChange your app on purpose, then try it on someone.

        Make it yours: start again

        There's a Clear button. Make it clear the Readings table.

        Add a Jupiter button: duplicate a Planet and set its g to 24.8. Or show how many readings there are with count(Readings).

        Try it yourselfYour edits are saved in this browser.

        Clear empties the Readings table.

          Run your program. This ticks when your code does all of the above.

          Now you can
          Finished the course?Go over it: Data apps recapEvery idea in one place, to check you've got it.You reached the endBack to Data apps
          For teachers

          Optional extension: an Embed shows a ready-made pendulum simulation (one web page, pendulum.html in this folder). The page sends period and length; the app sends gravity. Students build the app around it: Sim sends period shows the period (Build 1), planet buttons send gravity (Build 2), Record adds a row to Readings (Build 3). The Check shows that only the names in Sends get through. Then students use the lab to answer a science question and add a Clear button. About 45 minutes.

          Open the teacher guide: answers, solutions, slides and a worksheet.

          Students will be able to

          Before class

          Needs App Builder 1.13 (the Embed object and its messages, Level 4). The simulation is a single web page; the app never changes it. The page runs in a safe box: it can't see the rest of the app or anything saved in the browser, and the app only takes the message names listed in Sends, each with one plain value.

          The page is the same one as App Builder's Pendulum lab sample. You can make your own for another topic: in an Embed's Properties, fill in Sends and Listens for, then Copy a prompt for an AI chat gives a prompt that asks for a page using those names. Paste the page it gives back with + Paste HTML. Use only an AI chat your school allows; check the page works before a class uses it.

          The physics: a simple pendulum's period is 2π√(L/g), so it grows with length and shrinks with gravity, and doesn't depend on the bob's mass (for small swings). The page works the period out from that formula, so readings are exact; a real pendulum's would vary.

          Running the lesson

          1. Before Build 1 (whole class): open Sim's Properties. What does the simulation tell the app? What can the app tell it?
          2. Use your lab: collect a few rows from the class on the board and look for the pattern together.

          The hard part (on purpose)

          Seeing the simulation as a part with inputs and outputs: point at the Sends and Listens for boxes in Sim's Properties before Build 1.

          When students get stuck

          Stretch and support

          Stretch: Add a Sim sends length event that shows the length too. Why does the page send both, when the app could work out the period itself?

          Support: Build 1 together with the clip; pairs do Builds 2 and 3.

          Where it leads

          Science: Year 10 physics on forces and motion. Code: the web pathway's DOM course, where pages send and receive messages in the same way.

          What each step is for