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

add functionality to format CSS stylesheets #257

Merged
merged 1 commit into from
Dec 20, 2023
Merged

add functionality to format CSS stylesheets #257

merged 1 commit into from
Dec 20, 2023

Conversation

pmeier
Copy link
Member

@pmeier pmeier commented Dec 20, 2023

The more I look at our UI code, the more frustrated I grow about the way we define the stylesheets for the various panel components. Putting everything into one massive multiline string works, but has two downsides:

  1. The formatting is all over the place, since the auto-formatters won't format it at all.
  2. Inserting values is not easy, since in CSS braces, i.e. {}, are regular characters. Meaning, if we used f-strings, we would need to encode them all the time. We currently use .format() for this.

This PR adds a stylesheets function that takes two-tuples of CSS selectors and declarations. That way we can use regular Python syntax to define everything and fixing both issues above.

I've applied this to one instance of our code in this PR. If we move forward with this, I'll open an issue and asking for help of the community to fix the rest of the code.

@pmeier
Copy link
Member Author

pmeier commented Dec 20, 2023

For completion, here is what the output looks like:

from ragna.deploy._ui.styles import stylesheets

grid_height = 300

output = stylesheets(
    (":host", {"width": "100%"}),
    (
        ".pills_list",
        {
            # "background-color": "gold",
            "display": "grid",
            "grid-auto-flow": "row",
            "row-gap": "10px",
            "grid-template": f"repeat({grid_height}, 1fr) / repeat(3, 1fr)",
            "max-height": "200px",
            "overflow": "scroll",
        },
    ),
    (
        ".chat_document_pill",
        {
            "background-color": "rgb(241,241,241)",
            "margin-left": "5px",
            "margin-right": "5px",
            "padding": "5px 15px",
            "border-radius": "10px",
            "color": "var(--accent-color)",
            "width": "fit-content",
            "grid-column": "span 1",
        },
    ),
    ("ul", {"list-style-type": "none"}),
)


print("\n\n".join(output))
:host {
    width: 100%;
}

.pills_list {
    display: grid;
    grid-auto-flow: row;
    row-gap: 10px;
    grid-template: repeat(300, 1fr) / repeat(3, 1fr);
    max-height: 200px;
    overflow: scroll;
}

.chat_document_pill {
    background-color: rgb(241,241,241);
    margin-left: 5px;
    margin-right: 5px;
    padding: 5px 15px;
    border-radius: 10px;
    color: var(--accent-color);
    width: fit-content;
    grid-column: span 1;
}

ul {
    list-style-type: none;
}

Copy link
Contributor

@pierrotsmnrd pierrotsmnrd left a comment

Choose a reason for hiding this comment

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

The idea is great.

@pmeier pmeier merged commit 2056246 into main Dec 20, 2023
10 checks passed
@pmeier pmeier deleted the css branch December 20, 2023 09:51
@pmeier pmeier mentioned this pull request Feb 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants