Skip to content

Groups and Grid layouts

If you pass a list of blocks (such as Plot and Table) to a Report, they are -- by default -- laid out in a single column with a row per block. If you would like to customize the rows and columns, Datapane provides a Group block which takes a list of blocks and a number of columns and lays them out in a grid.

Info

As Groupblocks are blocks themselves, they are composable, and you can create more custom layers of nested blocks, for instance nesting 2 rows in the left column of a 2 column layout

Simple 2 column grid

import datapane as dp

report = dp.Report(
    dp.Group(dp.Text("⬅️ Left side"), dp.Text("➡️ Right side"), columns=2)
)

report.save(path="simple-grid.html")

Plot and DataTable in a 2 column grid

import pandas as pd
import altair as alt
import datapane as dp

alt.data_transformers.disable_max_rows()

dataset = pd.read_csv("https://covid.ourworldindata.org/data/owid-covid-data.csv")
df = (
    dataset.groupby(["continent", "date"])["new_cases_smoothed_per_million"]
    .mean()
    .reset_index()
)

plot = (
    alt.Chart(df)
    .mark_area(opacity=0.4, stroke="black")
    .encode(
        x="date:T",
        y=alt.Y("new_cases_smoothed_per_million:Q", stack=None),
        color=alt.Color("continent:N", scale=alt.Scale(scheme="set1")),
        tooltip="continent:N",
    )
    .interactive()
    .properties(width="container")
)

report = dp.Report(dp.Group(dp.Plot(plot), dp.DataTable(df), columns=2))
report = report.save(path="grid-layout.html")

Populating a grid with a list of Blocks

If you're generating your plots programmatically or have a lot of plots, you can pass them into the Group block as a list, using the blocks parameter. We can rewrite the previous example as follows :

import pandas as pd
import altair as alt
import datapane as dp

dataset = pd.read_csv("https://covid.ourworldindata.org/data/owid-covid-data.csv")
df = (
    dataset.groupby(["continent", "date"])["new_cases_smoothed_per_million"]
    .mean()
    .reset_index()
)

plot = (
    alt.Chart(df)
    .mark_area(opacity=0.4, stroke="black")
    .encode(
        x="date:T",
        y=alt.Y("new_cases_smoothed_per_million:Q", stack=None),
        color=alt.Color("continent:N", scale=alt.Scale(scheme="set1")),
        tooltip="continent:N",
    )
    .interactive()
    .properties(width="container")
)

# You could also generate these in a loop/function
my_plots = [dp.Plot(plot), dp.DataTable(df)]

report = dp.Report(dp.Group(blocks=my_plots, columns=2))

report.save(path="group-list.html")