-
Notifications
You must be signed in to change notification settings - Fork 787
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[cards] PythonCode
component
#2196
Conversation
fe04de4
to
9aef034
Compare
|
||
|
||
def create_component_id(component): | ||
uuid_bit = "".join(uuid.uuid4().hex.split("-"))[:6] | ||
return type(component).__name__.lower() + "_" + uuid_bit | ||
|
||
|
||
def with_default_component_id(func): | ||
def ret_func(self, *args, **kwargs): | ||
if self.component_id is None: | ||
self.component_id = create_component_id(self) | ||
return func(self, *args, **kwargs) | ||
|
||
return ret_func |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just moving things around. Nothing has changed overall.
|
||
|
||
def create_component_id(component): | ||
uuid_bit = "".join(uuid.uuid4().hex.split("-"))[:6] | ||
return type(component).__name__.lower() + "_" + uuid_bit | ||
|
||
|
||
def with_default_component_id(func): | ||
def ret_func(self, *args, **kwargs): | ||
if self.component_id is None: | ||
self.component_id = create_component_id(self) | ||
return func(self, *args, **kwargs) | ||
|
||
return ret_func |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just moved it to a common place so that it can be used in other modules as well.
- Metaflow Card component for rendering a syntax highlighed python function - Driveby: moved `with_component_id` and `create_component_id` functions to `card.py` - Default cards now have a code section present too - Passing flow object to MetaflowCard's init function so that the step function can be retrived by the card when required. - Any external cards that are using the static __init__ signature of the MetaflowCard will need to change it to the new signature. Any MetaflowCard's using **kwargs in the init signature are safe from the blast radius. - Example flow : ``` from metaflow import FlowSpec, step, card, current from metaflow.cards import PythonCode class PythonCodeDemoFlow(FlowSpec): @card @step def start(self): # Example 1: Using a function def sample_function(): x = 1 y = 2 return x + y current.card.append(PythonCode(sample_function)) # Example 2: Using a string code = """ def another_function(): return "Hello World" """ current.card.append(PythonCode(code_string=code)) self.next(self.end) @step def end(self): pass if __name__ == '__main__': PythonCodeDemoFlow() ```
9aef034
to
4356ff4
Compare
@@ -66,7 +67,7 @@ class MetaflowCard(object): | |||
# FIXME document runtime_data | |||
runtime_data = None | |||
|
|||
def __init__(self, options={}, components=[], graph=None): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Tweeking function signature here so that default card can access the step function and render it .
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Altered function signatures here for simplicity.
step_func = getattr(self.flow, self._task.parent.id) | ||
code_table = SectionComponent( | ||
title="Task Code", | ||
contents=[ | ||
TableComponent( | ||
data=[[PythonCodeComponent(inspect.getsource(step_func)).render()]] | ||
) | ||
], | ||
).render() | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Task code is now a part of the default card.
Metaflow Card component for rendering a syntax highlighed python function
Driveby: moved
with_component_id
andcreate_component_id
functions tocard.py
Default cards now have a code section present too
Passing flow object to MetaflowCard's init function so that the step function can be retrived by the card when required.
Any external cards that are using the static init signature of the MetaflowCard will need to change it to the new signature. Any MetaflowCard's using **kwargs in the init signature are safe from the blast radius.
Example flow :