How to create custom blocks
Create a new block type
To create a custom block type, define a class that subclasses Block
. The Block
base class builds
on Pydantic’s BaseModel
, so you can declare custom fields just like a Pydantic model.
We’ve already seen an example of a Cube
block that represents a cube and holds information about the length of each edge in inches:
Register custom blocks
In addition to the register_type_and_schema
method shown above, you can register blocks from a Python module with a CLI command:
This command is useful for registering all blocks found within a module in a Prefect Integration library.
Alternatively, if a custom block was created in a .py
file, you can register the block with the CLI command:
Block documents can now be created with the registered block schema.
Secret fields
All block values are encrypted before being stored.
If you have values that you would not like visible in the UI or in logs,
use the SecretStr
field type provided by Pydantic to automatically obfuscate those values.
You can use this capability for fields that store credentials such as passwords and API tokens.
Here’s an example of an AwsCredentials
block that uses SecretStr
:
Since aws_secret_access_key
has the SecretStr
type hint assigned to it,
the value of that field is not exposed if the object is logged:
Prefect’s SecretDict
field type allows you to add a dictionary field to your block
that automatically obfuscates values at all levels in the UI or in logs.
This capability is useful for blocks where typing or structure of secret fields is not known until configuration time.
Here’s an example of a block that uses SecretDict
:
system_secrets
is obfuscated when system_configuration_block
is displayed, but system_variables
show up in plain-text:
Customize a block’s display
You can set metadata fields on a block type’s subclass to control how a block displays.
Available metadata fields include:
Property | Description |
---|---|
_block_type_name | Display name of the block in the UI. Defaults to the class name. |
_block_type_slug | Unique slug used to reference the block type in the API. Defaults to a lowercase, dash-delimited version of the block type name. |
_logo_url | URL pointing to an image that should be displayed for the block type in the UI. Default to None . |
_description | Short description of block type. Defaults to docstring, if provided. |
_code_example | Short code snippet shown in UI for how to load/use block type. Defaults to first example provided in the docstring of the class, if provided. |
Update custom Block
types
Here’s an example of how to add a bucket_folder
field to your custom S3Bucket
block; it represents the default path to
read and write objects from (this field exists on our implementation).
Add the new field to the class definition:
Then register the updated block type with either your Prefect Cloud account or your self-hosted Prefect server instance.
If you have any existing blocks of this type that were created before the update and you’d prefer to not re-create them, migrate them to the new version of your block type by adding the missing values: