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

Generate XLSX from schemapack (GSI-676) #144

Open
wants to merge 13 commits into
base: refactor_schemapack
Choose a base branch
from

Conversation

lkuchenb
Copy link
Member

@lkuchenb lkuchenb commented Mar 15, 2024

This PR is a refactoring of the XLSX generator script to use schemapack instead of LinkML. Other than that the behaviour changes as follows:

  • The script no longer generates multiple output files but only one (full) submission XLSX
  • The order of the columns is now ID, relations, content properties. Originally the LinkML spec offered full control over the column ordering.

@lkuchenb lkuchenb requested a review from sbilge March 21, 2024 16:39
@lkuchenb lkuchenb marked this pull request as ready for review March 21, 2024 16:39
Copy link
Member

@sbilge sbilge left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we adhere to the practice of including docstrings for every function and class, or is it acceptable to omit them when the function is self-explanatory?

In addition, there are some inline comments and suggestions. Furthermore, I've included the submission.schemapack.yaml file in the repository to resolve failing checks, except for the linkml-specific schema-linter. We may consider removing the schema-linter, as it's no longer relevant. Or replace it with a schemapack-specific linter. However, I've implemented a temporary solution to utilize only the linkml schema, so keeping it shouldn't pose any issues for this PR.

requirements.txt Outdated Show resolved Hide resolved
from openpyxl.utils import get_column_letter
from pydantic import BaseModel, root_validator
import yaml
from pydantic import BaseModel
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
from pydantic import BaseModel
from pydantic import BaseModel, Field

"""A XLSX generator config"""

output_filename: str
styles: Optional[dict[str, WorksheetStyle]] = {}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
styles: Optional[dict[str, WorksheetStyle]] = {}
styles: dict[str, WorksheetStyle] = Field(default_factory=dict)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with the obsolete Optional but replace the plain default with a Field() and a factory?

scripts/generate_xlsx.py Outdated Show resolved Hide resolved
scripts/generate_xlsx.py Show resolved Hide resolved
Comment on lines +126 to +127
header_color: Optional[str] = None
content_color: Optional[str] = None
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Header and content color is defined as the attributes of the WorksheetStyle class previously. Maybe this would be better:

Suggested change
header_color: Optional[str] = None
content_color: Optional[str] = None
style: Optional[WorksheetStyle] = None

class Column:
name: str
description: Optional[str]
type: str
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Column type can be an enum, by that we would actually have control over the vocabulary. Smt like:
class ValueType(Enum): ARRAY = "array" ENUM = "enum" STRING = "string"

def _get_element_type_from_schema(schema: dict) -> str:
"""Get the type of the elements in an array from a JSON schema."""
element_type = schema.get("type", "object")
if element_type == "array":
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you accept the previous enum idea:

Suggested change
if element_type == "array":
if element_type == ValueType.ARRAY.value:

scripts/generate_xlsx.py Outdated Show resolved Hide resolved

cols = sheet.columns
rows = []
rows.append([Cell(wb_sheet, value=col.name) for col in cols])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I get type error type error from all the Cell related operations. Shall we suppress the with # type: ignore? I do not know if there is a way to suppress all the Cell related type errors through out this script.

@sbilge sbilge force-pushed the refactor_schemapack branch from 441a5e5 to a4afdf7 Compare May 14, 2024 12:48
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.

2 participants