Mohamed Alaa
09/21/2022, 4:20 PMfrom prefect.blocks.core import Block
cube = Block.load('Cube/cube-test')
where Cube is the name of the registered block and cube-test is an instance of it.
i would always get
ValueError: Unable to find block document named cube-test for block type Cube
I ran this code to find all the registered blocks
from prefect.utilities.dispatch import lookup_type, get_registry_for_type
from prefect.blocks.core import Block
registry = get_registry_for_type(Block)
print(registry)
And it results in all the default prefect block without my custom blocks
Am I missing something?
Thanks in advanceJeff Hale
09/21/2022, 4:33 PMprefect block ls
does the block you created appear?
If not, check that the profile that you using in your terminal matches where you created your block.Mohamed Alaa
09/22/2022, 6:45 AMprefect block ls
command i do see the blocks i created. The question is how do i access them in my code?Jeff Hale
09/22/2022, 12:41 PM# create custom block example
from prefect.blocks.core import Block
class Cube3(Block):
edge_length_inches: float
def get_volume(self):
return self.edge_length_inches**3
def get_surface_area(self):
return 6 * self.edge_length_inches**2
if __name__ == "__main__":
my_cube_object = Cube3(edge_length_inches=3)
my_cube_object.save(name="cubeit3")
Use the custom block in another file at the same directory level:
from prefect import flow
from customblock import Cube3
@flow
def testing():
cube_block = Cube3.load("cubeit3")
print(cube_block)
if __name__ == "__main__":
testing()
Note that here you are importing the class from the other file, so that file needs to be accessible.Mohamed Alaa
09/22/2022, 3:17 PM