Skip to content

Commit

Permalink
refactor: refactor deck module and unit tests
Browse files Browse the repository at this point in the history
- Import json and TableRenderer in `flytekit/deck/deck.py`
- Change how installed packages are fetched in `flytekit/deck/deck.py`
- Remove unused code in `flytekit/deck/deck.py`
- Remove assertions for "Library" and "Version" in `tests/flytekit/unit/deck/test_deck.py`

Signed-off-by: jason.lai <[email protected]>
  • Loading branch information
jasonlai1218 committed Mar 13, 2024
1 parent a4773e0 commit 05db9ab
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 19 deletions.
20 changes: 5 additions & 15 deletions flytekit/deck/deck.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,27 +186,17 @@ def __init__(self, name: Optional[str] = "Python Dependency", html: Optional[str

@property
def html(self) -> str:
try:
from flytekitplugins.deck.renderer import TableRenderer
except ImportError:
warning_info = "Plugin 'flytekit-deck-standard' is not installed. To display python dependency, install the plugin in the image."
logger.warning(warning_info)
return warning_info

import json
import subprocess

Check warning on line 190 in flytekit/deck/deck.py

View check run for this annotation

Codecov / codecov/patch

flytekit/deck/deck.py#L189-L190

Added lines #L189 - L190 were not covered by tests

import pandas as pd
from flytekitplugins.deck.renderer import TableRenderer

Check warning on line 193 in flytekit/deck/deck.py

View check run for this annotation

Codecov / codecov/patch

flytekit/deck/deck.py#L192-L193

Added lines #L192 - L193 were not covered by tests

try:
installed_packages = subprocess.check_output(["pip", "freeze"]).decode().split("\n")
installed_packages = json.loads(subprocess.check_output(["pip", "list", "--format", "json"]))
except subprocess.CalledProcessError as e:
logger.error(f"Error occurred while fetching installed packages: {e}")
return ""

columns_name = ["Library", "Version"]
installed_packages = [package.split("==") for package in installed_packages if package]
df = pd.DataFrame(installed_packages, columns=columns_name)
html = TableRenderer().to_html(df, header_labels=columns_name)
# Add CSS style to center align the table content
html = f"<div style='text-align: center;'>{html}</div>"
df = pd.DataFrame(installed_packages)
html = TableRenderer().to_html(df)
return html

Check warning on line 202 in flytekit/deck/deck.py

View check run for this annotation

Codecov / codecov/patch

flytekit/deck/deck.py#L195-L202

Added lines #L195 - L202 were not covered by tests
4 changes: 0 additions & 4 deletions tests/flytekit/unit/deck/test_deck.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,3 @@ def test_python_dependency_deck():
ctx.add_deck(python_dependency_deck)
assert len(ctx.user_space_params.decks) == 1
assert ctx.user_space_params.decks[0].name == "Python Dependency"

html_content = ctx.user_space_params.decks[0].html
assert "Library" in html_content
assert "Version" in html_content

0 comments on commit 05db9ab

Please sign in to comment.