Search⌘ K
AI Features

Exercise: Multiple Inputs and Outputs to Callbacks

Explore how to build Dash applications that handle multiple user inputs and outputs with callback functions. Learn to calculate and display values like squares, cubes, and percentage change interactively. This lesson guides you through chaining callbacks and updating UI elements in response to user input, enhancing the responsiveness of your dashboards.

We'll cover the following...

Exercise 1

Create a Dash app that allows the user to enter a number x as input. Now create two outputs, each of them as separate markdown components.

  • The first markdown component should read x² = [squared_num]
  • The second markdown component should read x³ = [cubed_num]

Here [squared_num] and [cubed_num] represent the computed values from the callback function calculation.

Solution

  • Two lambda functions are defined on line 1 and 2, respectively, for calculating the square and cube of a number, respectively.

  • The app layout is set using the dbc.Container method on line 8 which includes:

    • An H1 header element on line 9, displaying Exercise 1.

    • An Input element on line 11 from Dash Core Components, allowing users to input a number.

    • Two Markdown elements on line 12 and 13, respectively, from Dash Core Components with IDs of output1 and output2 to display ...