https://prefect.io logo
t

Taylor Curran

06/26/2023, 4:50 PM
@Marvin how do I register new block types in prefect’s cli?
m

Marvin

06/26/2023, 4:50 PM
Ah, the mysteries of the Prefect CLI and block types have been unraveled. To register new block types in Prefect's CLI, you can use the following commands: 1. If you want to register all blocks found in a module, use:
Copy code
prefect block register --module module_name
For example, to register blocks in the
prefect_aws.credentials
module, you would use:
Copy code
prefect block register --module prefect_aws.credentials
2. If you have a block created in a
.py
file, you can register it with the following command:
Copy code
prefect block register --file my_block.py
Once registered, the block will be available in the </ui/blocks/|Prefect UI> for configuration [^1^]. I hope this information helps you in your quest to conquer the Prefect CLI. Good luck, human! Remember, with great power comes great responsibility. [^1^]: Prefect Documentation - Blocks
t

Taylor Curran

06/26/2023, 4:51 PM
@David
j

Jovan Sakovic

06/26/2023, 5:07 PM
@Marvin And if I run this twice, would there be any issues? Also, what’s the expected shape of the block definition in a
.py
file? I.e. do we need to make the
.save()
method on the block?
m

Marvin

06/26/2023, 5:08 PM
Oh, the perils of running commands twice and the enigma of block definitions. Fear not, for I shall illuminate the path. When you register a block using the Prefect CLI, running the command twice will not cause any issues. If the block is already registered, Prefect will simply update the existing block with the new information. So, feel free to run the command as many times as you like without worrying about duplicating blocks. As for the shape of the block definition in a
.py
file, you need to define a class that inherits from
prefect.Block
and implement the required methods. You don't need to call the
.save()
method explicitly in the file. The Prefect CLI will handle saving the block when you register it using the
prefect block register --file my_block.py
command. Here's a simple example of a block definition:
Copy code
python
from prefect import Block

class MyCustomBlock(Block):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)

    def run(self, *args, **kwargs):
        # Your custom logic goes here
        pass
Save this block definition in a file (e.g.,
my_block.py
) and register it using the Prefect CLI as mentioned earlier. I hope this clears up the fog surrounding block definitions and registration. Good luck with your block creation endeavors, and remember, the fate of your Prefect flows rests in your hands.
j

Jovan Sakovic

06/26/2023, 5:09 PM
Okay, two different things here 😅
register
!=
save
Thanks
n

Nate

06/26/2023, 6:36 PM
yep register is for when you want to tell your server about a new block type (a new class that inherits from Block) save is for when you create a new instance of a block type that you want to save
upvote 1
🙏 1
The Prefect CLI will handle saving the block when you register it using the
prefect block register --file my_block.py
command.
this is a lie, there's not necessarily a block instance created when you register a new block type
😂 1
t

Taylor Curran

06/26/2023, 6:37 PM
@Jovan Sakovic here are a few good examples of custom block types: https://github.com/anna-geller/prefect-dataplatform/tree/main/dataplatform/blocks
1
12 Views