Skip to content
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

Merged
merged 1 commit into from
Jan 10, 2025

Conversation

valayDave
Copy link
Collaborator

@valayDave valayDave commented Jan 6, 2025

  • 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()

@valayDave valayDave force-pushed the valay/oss-code-component branch from fe04de4 to 9aef034 Compare January 7, 2025 08:00
@valayDave valayDave requested a review from romain-intel January 7, 2025 08:01
Comment on lines +144 to +157


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
Copy link
Collaborator Author

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.

Comment on lines -15 to -28


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
Copy link
Collaborator Author

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/plugins/cards/card_modules/components.py Outdated Show resolved Hide resolved
- 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()
```
@valayDave valayDave force-pushed the valay/oss-code-component branch from 9aef034 to 4356ff4 Compare January 10, 2025 09:39
@@ -66,7 +67,7 @@ class MetaflowCard(object):
# FIXME document runtime_data
runtime_data = None

def __init__(self, options={}, components=[], graph=None):
Copy link
Collaborator Author

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 .

Copy link
Collaborator Author

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.

Comment on lines +500 to +509
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()

Copy link
Collaborator Author

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.

@savingoyal savingoyal merged commit 2fb29ae into Netflix:master Jan 10, 2025
29 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants