Mohamed Alaa09/21/2022, 4:20 PM
where Cube is the name of the registered block and cube-test is an instance of it. i would always get
from prefect.blocks.core import Block cube = Block.load('Cube/cube-test')
I ran this code to find all the registered blocks
ValueError: Unable to find block document named cube-test for block type Cube
And it results in all the default prefect block without my custom blocks Am I missing something? Thanks in advance
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)
Jeff Hale09/21/2022, 4:33 PM
does the block you created appear? If not, check that the profile that you using in your terminal matches where you created your block.
prefect block ls
Mohamed Alaa09/22/2022, 6:45 AM
command i do see the blocks i created. The question is how do i access them in my code?
prefect block ls
Jeff Hale09/22/2022, 12:41 PM
Use the custom block in another file at the same directory level:
# 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")
Note that here you are importing the class from the other file, so that file needs to be accessible.
from prefect import flow from customblock import Cube3 @flow def testing(): cube_block = Cube3.load("cubeit3") print(cube_block) if __name__ == "__main__": testing()
Mohamed Alaa09/22/2022, 3:17 PM