Skip to main content

Current - Operating a run

The current object is used to inspect and manipulate the currently executing run. It is only available during flow execution, i.e. inside a FlowSpec class and functions called from its steps. You can access the object simply by importing it: from metaflow import current.

The attributes available in current depend on the decorators assigned to the flow and the step where current is used. Attributes that are always available are listed under Common Attributes below. Decorator-specific attributes are listed under the decorator name.

Common Attributes

These attributes are always available in the current object.

current.is_running_flow

[source]

Returns True if called inside a running Flow, False otherwise.

You can use this property e.g. inside a library to choose the desired behavior depending on the execution context.

Returns 

bool

True if called inside a run, False otherwise.

current.flow_name

[source]

The name of the currently executing flow.

Returns 

str, optional

Flow name.

current.run_id

[source]

The run ID of the currently executing run.

Returns 

str, optional

Run ID.

current.step_name

[source]

The name of the currently executing step.

Returns 

str, optional

Step name.

current.task_id

[source]

The task ID of the currently executing task.

Returns 

str, optional

Task ID.

current.retry_count

[source]

The index of the task execution attempt.

This property returns 0 for the first attempt to execute the task. If the @retry decorator is used and the first attempt fails, this property returns the number of times the task was attempted prior to the current attempt.

Returns 

int

The retry count.

current.origin_run_id

[source]

The run ID of the original run this run was resumed from.

This property returns None for ordinary runs. If the run was started by the resume command, the property returns the ID of the original run.

You can use this property to detect if the run is resumed or not.

Returns 

str, optional

Run ID of the original run.

current.pathspec

[source]

Pathspec of the current task, i.e. a unique identifier of the current task. The returned string follows this format:

{flow_name}/{run_id}/{step_name}/{task_id}

This is a shorthand to current.task.pathspec.

Returns 

str, optional

Pathspec.

current.namespace

[source]

The current namespace.

Returns 

str

Namespace.

current.username

[source]

The name of the user who started the run, if available.

Returns 

str, optional

User name.

current.tempdir

[source]

Currently configured temporary directory.

Returns 

str, optional

Temporary director.

Decorator-specific attributes

These attributes are only available when the decorator is present.

@project

The @project decorator exposes attributes related to the current deployment.

current.project_name

[source]

The name of the project assigned to this flow, i.e. X in @project(name=X).

Returns 

str

Project name.

current.project_flow_name

[source]

The flow name prefixed with the current project and branch. This name identifies the deployment on a production scheduler.

Returns 

str

Flow name prefixed with project information.

current.branch_name

[source]

The current branch, i.e. X in --branch=X set during deployment.

Returns 

str

Branch name.

current.is_user_branch

[source]

True if the flow is deployed without a specific --branch or a --production flag.

Returns 

bool

True if the deployment does not correspond to a specific branch.

current.is_production

[source]

True if the flow is deployed with the --production flag.

Returns 

bool

True if the flow is deployed in --production.

@card

The @card decorator exposes functions in current that allow you to customize the contents of cards using card components. For an overview of card-related APIs, see the API reference for cards.

current.card.__getitem__(self)

[source]

Choose a specific card for manipulation.

When multiple @card decorators are present, you can add an ID to distinguish between them, @card(id=ID). This allows you to add components to a specific card like this:

current.card[ID].append(component)
Parameters 

key: str

Card ID.

Returns 

CardComponentManager

An object with append and extend calls which allow you to add components to the chosen card.

current.card.__setitem__(self)

[source]

Specify components of the chosen card.

Instead of adding components to a card individually with current.card[ID].append(component), use this method to assign a list of components to a card, replacing the existing components:

current.card[ID] = [FirstComponent, SecondComponent]
Parameters 

key: str

Card ID.

value: List[MetaflowCardComponent]

List of card components to assign to this card.

current.card.append(self)

[source]

Appends a component to the current card.

Parameters 

component: MetaflowCardComponent

Card component to add to this card.

current.card.extend(self)

[source]

Appends many components to the current card.

Parameters 

component: Iterator[MetaflowCardComponent]

Card components to add to this card.

current.card.clear(self)

[source]

Clears the list of components in this card.

current.card.refresh(self)

[source]

Schedule the contents of this dynamic card to be refreshed soon.

Call this method after you have modified the list of components or changed their contents with the components' update method. This will cause the card to update live while the task is executing.

Note that this method is rate-limited, determined by the @card(refresh_interval) attribute. If you call this method more frequently than what is allowed by refresh_interval, some calls are ignored causing the card to update more slowly.

It is advisable not to call this method more often than once a second, which is the minimum allowed value for refresh_interval.

Parameters 

data: dict, optional

Optional user-defined data to be passed to a custom card.

force: bool, optional

Force a full card re-render, not just a data update.

@trigger and @trigger_on_finish

You can inspect event(s) that triggered an event-triggered run through current.trigger which returns a MetaflowTrigger object, if the run was triggered by an event.

current.trigger

[source]

Returns MetaflowTrigger if the current run is triggered by an event.

Returns 

MetaflowTrigger

MetaflowTrigger if triggered by a run.