Skip to main content
When a training step runs for hours, problems don’t always show up as errors. The model might stop improving halfway through, or the step might slow to a crawl without failing. If you find out only after training finishes, you’ve already paid for compute that didn’t help. If you catch the problem while the step is still running, you can stop it early and adjust your settings for the next run. The usual ways to watch a running step each come with a tradeoff. Print statements take one line to add, but they produce a stream of numbers that’s hard to read at a glance, so spotting a trend takes work. A dashboard or experiment-tracking service turns those numbers into charts, but it’s another service to set up and maintain, and its view of a run can drift from the code that produced it. With a few lines of Python, the @card decorator adds progress bars, metrics, and charts to a step and updates them while the step runs, without needing to set up a separate service. You can watch the card while the step progresses. Because the card is stored with the task, it still shows what the step reported after the run finishes, so it can’t drift from the code that produced it.
The local card viewer at localhost:8324, showing the training card with its status heading, a completed progress bar, the best validation log loss, and a line chart of training and validation loss across 500 rounds

Prerequisites

Before you begin, make sure you have the following:

How cards work

By default, adding the @card decorator to a step produces a visual report of the step after it finishes. However, you can instruct your flow to update the card as the step runs, allowing you to watch the step complete in real time. You can view the card in the Runs view, or in Metaflow’s local card viewer, which shows cards in your browser without the platform UI. Cards display components that you configure, such as blocks of text, progress bars, and charts. Each component is a Python object that you create in your step’s code. To configure a card to update while a step runs:
1

Attach a card to the step

@card(type="blank") attaches an empty card that you fill with components. Without type="blank", Metaflow uses the default card, which also lists the step’s artifacts.
2

Add components to the card

There are two methods for adding components to your card:
  • current.card.append(<COMPONENT>) adds one component
  • current.card.extend([<COMPONENT_1>, <COMPONENT_2>]) adds a list of components
Components appear on the card in the order you add them.
3

Update component values

Each component has its own update() method, which sets a new value for that component, such as a progress bar’s position or the text in a Markdown block. Call it from the loop or callback where your step does its work.
4

Refresh the card

New values received from calling update() stay in your step’s memory until you refresh the card. current.card.refresh() sends the current state of every component to the viewer. Call it after your updates, in the same loop.
Minimal training loop example
The card in this tutorial uses four built-in components:

Building a step that updates its card

In this section, you’ll create two files: a module that builds and updates the card during XGBoost training and the flow that uses it.
1

Create a project directory

Create a directory for the tutorial and navigate to it:
Metaflow packages every Python file in this directory with your flow, so the flow can import the module you create next, both locally and in the cloud.
2

Create the card callback

XGBoost calls a training callback after every boosting round, along with the latest evaluation scores. That makes a callback a natural place to update the card. Create a file named live_card.py with the following contents:
live_card.py
The file defines two callbacks:
  • CardUpdater: Builds the card and keeps it current. The highlighted lines are where it works with the card:
    • When training starts, it creates the four components, adds them to the card with current.card.extend(), and calls refresh() so the card appears before the first round finishes.
    • After each round, it adds the latest training and validation loss to the chart’s data, calls update() on each component, and then calls refresh().
    • When training ends, it updates the status heading with the total training time.
  • SlowDown: Pauses after each round so that training lasts about two minutes, long enough to watch the card change. The sample dataset trains in seconds on most machines. Leave this callback out of your own projects.
The chart plots loss on a logarithmic scale, so small changes late in training stay visible next to the large drop in the first few rounds.
3

Create the flow

Create a file named live_card_flow.py with the following contents:
live_card_flow.py
The flow has two parameters you can change on the command line. --rounds sets the total number of boosting rounds, and --pause sets the seconds to wait after each round. The default pause of 0.2 seconds makes the run last about two minutes. Set --pause 0 to disable it.The highlighted lines connect the card to training:
  • @card(type="blank", id="training", refresh_interval=1) attaches an empty card to the train step that updates at most once per second. The id gives the card a name, which the Runs view uses as the card’s tab label.
  • The callbacks list passes CardUpdater to XGBoost, along with SlowDown unless you set --pause 0.
  • The evals argument tells XGBoost to score the model on the training and validation data after every round. Those scores arrive in the callback as evals_log, a dictionary keyed by dataset name and then by metric name.
The start step generates a sample classification dataset with scikit-learn and splits it into training and validation sets. @anaconda_base declares the packages every step needs, sourced from Anaconda’s main channel. After training, Metaflow saves self.model as an artifact of the train task, which you can load later with the Client API.
Your live-card-tutorial directory now contains live_card.py and live_card_flow.py.

Watching the card locally

Metaflow includes a local card viewer that shows cards in your browser as a flow runs. It’s the quickest way to check a card while you’re developing it.
1

Start the card viewer

From the live-card-tutorial directory, start the card viewer:
The viewer follows the latest run of LiveCardFlow and switches to each new run automatically when it starts. The --poll-interval 1 option checks for card updates every second instead of every five seconds.Leave the viewer running, and open http://localhost:8324 in your browser.
2

Run the flow

Open a second terminal, navigate to the live-card-tutorial directory, and run the flow:
The first run takes a few minutes longer than subsequent runs because Metaflow needs to build the environment declared in @anaconda_base.
3

Watch the card update

When the train step starts, return to your browser. The viewer shows the card for the train task and updates it every second as training progresses.As the card updates, watch the two lines on the chart. Training loss keeps falling for the whole run, but validation loss flattens out near 0.09 within the first hundred rounds, and the gap between the two lines keeps growing. The Best validation log loss box stops changing at round 194 and holds that score for the rest of the run, with Round 194 underneath. The growing gap is the model learning details of the training data that don’t carry over to new data. In a real training job, a card like this tells you while the job is still running that the remaining rounds won’t improve the model, so you can stop early or adjust your settings.Expected result: The card shows Training finished in followed by the total time, and the progress bar shows 500/500. In the second terminal, the run ends with Trained 500 rounds.When you’re done, press Ctrl+C in the first terminal to stop the card viewer.

Watching the card in the cloud

The card works the same way when the step runs on cloud compute. The task sends card updates to your platform’s storage as it runs, and the Runs view shows them, so you can check on a long training job from anywhere without keeping a terminal open.
1

Run the flow on cloud compute

Run the same flow on cloud compute. You don’t need to change any code:
Anaconda Platform builds the environment into a container image and runs each step in its own container on the cluster. The first run takes a few minutes while the image builds and the cluster starts the containers.
2

Open the card in the Runs view

When the output shows that the train step has started, follow the link printed at the start of the run to view it in the UI. Select the train task, then select the Cards tab.
Viewing the card in the Platform UI as it updates
Expected result: The training card shows the same components as in the local viewer and updates while the task runs. After the run finishes, the card stays with the task, so you can return to it at any time to see how training went.

Tutorial wrap up

You’ve built a card that reports on training while it happens. The card showed training progress, the best validation score, and the loss curves as each round finished, first in the local card viewer and then in the Runs view while the step ran on cloud compute. What you learned
  • How a card behaves differently when you update it while a step runs
  • How to add components to a card with current.card.extend() and keep them current with update() and current.card.refresh()
  • How to use a training callback to update a card after every round
  • How to watch cards locally with the card viewer and in the Runs view
  • How a chart of the loss curves shows when more training stops helping
Next steps
  • The dynamic card gallery includes monitor-callback, which uses the same callback pattern as this tutorial. It also shows work in separate threads and subprocesses, and charts built with Altair.
  • Card templates explain how to build fully custom cards with any JavaScript library and share them as Python packages. The 3D scatter plot card is a starter template for a custom card.
  • Training models that recover from interruptions shows how to resume a failed training step from its latest checkpoint, which pairs well with an updating card on long training jobs.
Have feedback? Use the / feedback buttons below to let us know what you thought about this tutorial and help us improve.