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

feat: add source code deck #2038

Merged
merged 9 commits into from
Dec 15, 2023
30 changes: 30 additions & 0 deletions plugins/flytekit-deck-standard/flytekitplugins/deck/renderer.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,36 @@
PIL = lazy_module("PIL")


class SourceCodeRenderer:
"""
Convert Python source code to HTML, and return HTML as a unicode string.
"""

def __init__(self, title: str = "Source Code"):
self._title = title

def to_html(self, source_code: str) -> str:
"""
Convert the provided Python source code into HTML format using Pygments library.

This method applies a colorful style and replaces the color "#fff0f0" with "#ffffff" in CSS.

Args:
source_code (str): The Python source code to be converted.

Returns:
str: The resulting HTML as a string, including CSS and highlighted source code.
"""
from pygments import highlight
from pygments.formatters.html import HtmlFormatter
from pygments.lexers.python import PythonLexer

formatter = HtmlFormatter(style="colorful")
css = formatter.get_style_defs(".highlight").replace("#fff0f0", "#ffffff")
html = highlight(source_code, PythonLexer(), formatter)
return f"<style>{css}</style>{html}"


class FrameProfilingRenderer:
"""
Generate a ProfileReport based on a pandas DataFrame
Expand Down
9 changes: 8 additions & 1 deletion plugins/flytekit-deck-standard/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,14 @@

microlib_name = f"flytekitplugins-{PLUGIN_NAME}-standard"

plugin_requires = ["flytekit>=1.3.0b2,<2.0.0", "markdown", "plotly", "ydata-profiling", "ipywidgets"]
plugin_requires = [
"flytekit",
"markdown",
"plotly",
"ydata-profiling",
"ipywidgets",
"pygments",
]

__version__ = "0.0.0+develop"

Expand Down
15 changes: 15 additions & 0 deletions plugins/flytekit-deck-standard/tests/test_renderer.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
GanttChartRenderer,
ImageRenderer,
MarkdownRenderer,
SourceCodeRenderer,
TableRenderer,
)
from PIL import Image
Expand Down Expand Up @@ -80,3 +81,17 @@ def test_table_renderer():
def test_gantt_chart_renderer():
renderer = GanttChartRenderer()
assert "Plotlyconfig = {Mathjaxconfig: 'Local'}" in renderer.to_html(time_info_df).title()


def test_source_code_renderer():
renderer = SourceCodeRenderer()
source_code = "def hello_world():\n print('Hello, world!')"
result = renderer.to_html(source_code)

# Assert that the result includes parts of the source code
assert "hello_world" in result
assert "Hello, world!" in result

# Assert that the color #ffffff is used instead of #fff0f0
assert "#ffffff" in result
assert "#fff0f0" not in result
Loading