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
43 changes: 43 additions & 0 deletions plugins/flytekit-deck-standard/flytekitplugins/deck/renderer.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
from typing import TYPE_CHECKING, List, Optional, Union

from pygments import highlight
from pygments.formatters.html import HtmlFormatter
from pygments.lexers.python import PythonLexer
pingsutw marked this conversation as resolved.
Show resolved Hide resolved

from flytekit import lazy_module
from flytekit.types.file import FlyteFile

Expand All @@ -15,6 +19,45 @@
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

Check warning on line 28 in plugins/flytekit-deck-standard/flytekitplugins/deck/renderer.py

View check run for this annotation

Codecov / codecov/patch

plugins/flytekit-deck-standard/flytekitplugins/deck/renderer.py#L28

Added line #L28 was not covered by tests

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.
"""
formatter = HtmlFormatter(style="colorful")
css = self._get_css(formatter)
html = highlight(source_code, PythonLexer(), formatter)
return f"<style>{css}</style>{html}"

Check warning on line 45 in plugins/flytekit-deck-standard/flytekitplugins/deck/renderer.py

View check run for this annotation

Codecov / codecov/patch

plugins/flytekit-deck-standard/flytekitplugins/deck/renderer.py#L42-L45

Added lines #L42 - L45 were not covered by tests

@staticmethod
def _get_css(formatter: HtmlFormatter) -> str:
"""
Get the CSS from the provided HtmlFormatter instance and replace the color "#fff0f0" with "#ffffff".

Args:
formatter (HtmlFormatter): An instance of HtmlFormatter.

Returns:
str: The CSS string with "#fff0f0" replaced by "#ffffff".
"""
return formatter.get_style_defs(".highlight").replace("#fff0f0", "#ffffff")


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 @@ -4,13 +4,16 @@
import markdown
import pandas as pd
import pytest
from pygments.formatters.html import HtmlFormatter

from flytekitplugins.deck.renderer import (
BoxRenderer,
FrameProfilingRenderer,
GanttChartRenderer,
ImageRenderer,
MarkdownRenderer,
TableRenderer,
SourceCodeRenderer,
)
from PIL import Image

Expand Down Expand Up @@ -80,3 +83,15 @@ 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_get_css():
formatter = HtmlFormatter(style="colorful")
css = SourceCodeRenderer._get_css(formatter)

# Check if the returned CSS contains the expected style
assert ".highlight" in css

# Check if the color "#fff0f0" has been replaced with "#ffffff"
assert "#fff0f0" not in css
assert "#ffffff" in css