Skip to content

Issue #138: Add configurable theme settings and custom CSS injection#196

Closed
onemriganka wants to merge 1 commit intoOpenMS:mainfrom
onemriganka:issue-138-theming
Closed

Issue #138: Add configurable theme settings and custom CSS injection#196
onemriganka wants to merge 1 commit intoOpenMS:mainfrom
onemriganka:issue-138-theming

Conversation

@onemriganka
Copy link

@onemriganka onemriganka commented Mar 22, 2025

PR Title:
Enhance Theme Customization via Config File (Fixes #138)

PR Description:
This pull request addresses issue [#138] by introducing an external configuration for theming, which enables users to customize the app’s appearance without modifying core code. The changes include:

  • New config.py File:
    A new file is added at the root level that defines theme settings (primaryColor, backgroundColor, secondaryBackgroundColor, textColor, and font). These settings can be overridden via environment variables, ensuring flexibility for different deployment scenarios.

  • Custom CSS Injection in app.py:
    The main application file (app.py) is modified to import the theme configuration from config.py and inject a custom CSS block based on these settings. This ensures the entire app adheres to the specified theme without affecting the existing navigation and page rendering logic.

  • Backward Compatibility:
    The changes are additive and do not alter the core functionality of the app. If no environment variables are provided, default values are used, ensuring the existing look is maintained.

  • Documentation Updates:
    Instructions have been added within this PR’s notes to inform users how to customize theme settings using environment variables.

Testing Instructions:

  1. Clone the repository and switch to this branch.
  2. Create or update environment variables to test different themes, e.g.:
    export STREAMLIT_BACKGROUND_COLOR="#EFEFEF"
    export STREAMLIT_TEXT_COLOR="#333333"
    export STREAMLIT_FONT="Arial"
    streamlit run app.py
  3. Verify that the custom CSS is applied and the app renders as expected.

This approach modularizes theme customization, making future enhancements straightforward and providing a more user-friendly configuration method.

Summary by CodeRabbit

  • New Features
    • Enabled dynamic theme customization, allowing the user interface to reflect personalized style settings based on customizable theme configurations.
    • Introduced custom styling adjustments for key UI components, enhancing the overall visual experience.

@coderabbitai
Copy link

coderabbitai bot commented Mar 22, 2025

Walkthrough

This change introduces a dynamic theming feature for the Streamlit application. The application now imports a theme configuration dictionary (THEME_CONFIG) from a configuration file. Custom CSS is constructed using properties from this dictionary and injected into the app via Streamlit’s markdown mechanism. In parallel, config.py now defines THEME_CONFIG, sourcing its values from environment variables and providing default values to customize the UI styling.

Changes

File(s) Change Summary
app.py, config.py Introduced dynamic CSS theming: app.py imports THEME_CONFIG and injects custom CSS into the Streamlit app, while config.py now defines THEME_CONFIG using environment variables with default values.

Sequence Diagram(s)

sequenceDiagram
    participant U as User
    participant A as app.py
    participant C as config.py
    participant S as Streamlit

    U->>A: Launch Application
    A->>C: Import THEME_CONFIG
    C-->>A: Return theme configuration
    A->>A: Create custom_css using THEME_CONFIG values
    A->>S: Inject CSS using st.markdown
    S-->>U: Render styled UI
Loading

Assessment against linked issues

Objective Addressed Explanation
Implement additional tests for TOPP tool pages (#138) No tests or test modifications were introduced in this PR.

Poem

I'm a bouncy rabbit, hopping through the code,
With new themes so vibrant on the digital road.
CSS magic sprinkles a stylish beat,
Making the app's look simply hard to beat.
🐇 Cheers to fresh styles and a UI so neat!

✨ Finishing Touches
  • 📝 Generate Docstrings

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🧹 Nitpick comments (4)
config.py (2)

5-9: Consider extracting default values as constants.

To improve maintainability, consider extracting the default color values as named constants at the top of the file. This would make future updates easier and provide a single place to modify defaults.

import os

+ # Default theme colors and fonts
+ DEFAULT_PRIMARY_COLOR = "#F63366"
+ DEFAULT_BACKGROUND_COLOR = "#FFFFFF"
+ DEFAULT_SECONDARY_BG_COLOR = "#F0F2F6"
+ DEFAULT_TEXT_COLOR = "#000000"
+ DEFAULT_FONT = "sans serif"
+
# Theme configuration with defaults; these can be overridden via environment variables.
THEME_CONFIG = {
-    "primaryColor": os.getenv("STREAMLIT_PRIMARY_COLOR", "#F63366"),
-    "backgroundColor": os.getenv("STREAMLIT_BACKGROUND_COLOR", "#FFFFFF"),
-    "secondaryBackgroundColor": os.getenv("STREAMLIT_SECONDARY_BACKGROUND_COLOR", "#F0F2F6"),
-    "textColor": os.getenv("STREAMLIT_TEXT_COLOR", "#000000"),
-    "font": os.getenv("STREAMLIT_FONT", "sans serif")
+    "primaryColor": os.getenv("STREAMLIT_PRIMARY_COLOR", DEFAULT_PRIMARY_COLOR),
+    "backgroundColor": os.getenv("STREAMLIT_BACKGROUND_COLOR", DEFAULT_BACKGROUND_COLOR),
+    "secondaryBackgroundColor": os.getenv("STREAMLIT_SECONDARY_BACKGROUND_COLOR", DEFAULT_SECONDARY_BG_COLOR),
+    "textColor": os.getenv("STREAMLIT_TEXT_COLOR", DEFAULT_TEXT_COLOR),
+    "font": os.getenv("STREAMLIT_FONT", DEFAULT_FONT)
}

5-5: The primaryColor is defined but not used in the CSS.

The THEME_CONFIG includes a primaryColor key, but it's not being used in the custom CSS in app.py. Either use this color in the CSS or remove it from the configuration to maintain consistency.

app.py (2)

28-28: Security consideration with unsafe_allow_html.

Using unsafe_allow_html=True is necessary for CSS injection but opens potential security risks if the input isn't properly controlled. Since you're using environment variables with predefined defaults, the risk is minimal, but it's good practice to add a comment explaining the security implications.

# Render the custom CSS on the page
+ # Note: unsafe_allow_html is required for CSS injection and is safe here as we control the input
st.markdown(custom_css, unsafe_allow_html=True)

11-25: Consider enhancing CSS structure with additional comments and organization.

The CSS structure could be improved with more comprehensive comments and logical grouping of styles. This would make future additions easier.

# Inject custom CSS using the theme configuration
custom_css = f"""
<style>
+ /* 
+  * Global theme settings
+  * These styles apply to the main components of the Streamlit UI
+  */
+
/* Container styling */
.reportview-container {{
    background-color: {THEME_CONFIG['backgroundColor']};
    color: {THEME_CONFIG['textColor']};
    font-family: {THEME_CONFIG['font']};
}}

/* Sidebar styling */
.sidebar .sidebar-content {{
    background: {THEME_CONFIG['secondaryBackgroundColor']};
}}
</style>
"""
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 6a7766a and d9c0bb5.

📒 Files selected for processing (2)
  • app.py (1 hunks)
  • config.py (1 hunks)
🔇 Additional comments (4)
config.py (1)

1-10: Well-structured theme configuration with environment variable support.

The implementation allows for flexible theme customization through environment variables with sensible defaults, which aligns well with the PR objectives.

app.py (3)

7-9: Clean import with good comment.

The import statement is clear and well-commented, making the source of the theme configuration obvious.


21-23: Same concern with sidebar CSS selector.

The .sidebar .sidebar-content selector might also be subject to change in future Streamlit updates. Consider using more stable selectors if available.


14-18:

Details

✅ Verification successful

Streamlit CSS class selector may change in future versions.

The .reportview-container class is being used for styling, but Streamlit's internal class names might change in future updates. Consider using more stable selectors or check Streamlit's documentation for recommended theming approaches.


🌐 Web query:

What are the recommended ways to customize Streamlit UI with CSS?

💡 Result:

To customize the appearance of a Streamlit app using CSS, here are the recommended approaches and considerations based on community practices and official guidance:


1. Inline CSS via st.markdown

Embed CSS directly in your app using st.markdown with unsafe_allow_html=True:

st.markdown("""
    <style>
        /* Customize headers */
        h1 {
            font-size: 24px !important;
            color: #2E86C1;
        }
        /* Style buttons */
        .stButton > button {
            background: linear-gradient(45deg, #4CAF50, #45a049);
            border-radius: 8px;
        }
    </style>
""", unsafe_allow_html=True)

This method is simple but requires caution since Streamlit’s HTML/CSS structure may change in updates, breaking styles[1][10].


2. External CSS Files

Load styles from a separate CSS file for cleaner code:

# Import CSS from an external file
def load_css(file_path):
    with open(file_path, "r") as f:
        css = f.read()
    st.markdown(f"<style>{css}</style>", unsafe_allow_html=True)

load_css("styles.css")  # Call this in your main script

This approach is ideal for larger projects (e.g., GitHub templates like Streamlit UI Template) and promotes reusability[6].


3. Targeting Specific Elements

Use browser DevTools to inspect Streamlit’s auto-generated class/ID names (e.g., .stButton, .stTextInput) and apply styles:

/* Style input boxes */
.stTextInput input {
    border: 2px solid #3498DB !important;
    padding: 10px !important;
}

/* Modify metric widgets */
[data-testid="stMetric"] {
    background: #F8F9F9;
    border: 1px solid #D0D3D4;
    border-radius: 10px;
}

Note: Class/ID names (e.g., st-emotion-cache-zt5igj) are unstable and may change with updates[12][14].


4. Native Theming (Non-CSS)

For foundational styling, configure the config.toml file:

[theme]
primaryColor = "#4CAF50"
backgroundColor = "#FFFFFF"
secondaryBackgroundColor = "#F0F2F6"
textColor = "#31333F"
font = "sans serif"

This officially supported method controls colors, fonts, and widget accents globally[2][8].


5. Third-Party Components

Leverage community-built tools like:

  • streamlit-extras for badges, cards, and spacers.
  • streamlit.components.v1.html to inject custom HTML/CSS templates[1][9].

Key Considerations

  • Avoid dependency on auto-generated classes: Streamlit’s internal class names (e.g., .st-emotion-cache-1vbkxwb) are not stable across versions[14].
  • Use !important sparingly: Override default styles carefully to avoid conflicts.
  • Test across themes: Ensure CSS works in both light/dark modes[9].
  • Performance: Heavy CSS animations or external fonts may slow load times[11].

For detailed tutorials, see Streamlit’s Theming Guide or community videos like Styling with Custom CSS[4][10].

Citations:


Attention: Update Styling Approach for Streamlit's Future Compatibility

File: app.py
Lines: 14–18

.reportview-container {
    background-color: {THEME_CONFIG['backgroundColor']};
    color: {THEME_CONFIG['textColor']};
    font-family: {THEME_CONFIG['font']};
}

While this styling currently works, note that the .reportview-container class is an internal selector generated by Streamlit and may change in future releases. As a more future-proof alternative, consider one of the following approaches:

  • Inline CSS via st.markdown: Inject styles directly into your app using st.markdown with the unsafe_allow_html=True flag.
  • External CSS Files: Maintain your styles in a separate CSS file and load them at runtime.
  • Native Theming: Leverage Streamlit’s official theming support in the config.toml file for global styling.

Refer to Streamlit’s Theming Guide for detailed recommendations to ensure your UI remains stable across updates.

Comment on lines +10 to +25
# Inject custom CSS using the theme configuration
custom_css = f"""
<style>
/* Container styling */
.reportview-container {{
background-color: {THEME_CONFIG['backgroundColor']};
color: {THEME_CONFIG['textColor']};
font-family: {THEME_CONFIG['font']};
}}

/* Sidebar styling */
.sidebar .sidebar-content {{
background: {THEME_CONFIG['secondaryBackgroundColor']};
}}
</style>
"""
Copy link

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Missing usage of primaryColor in custom CSS.

The custom CSS does not use the primaryColor from THEME_CONFIG even though it's defined in the configuration. Consider applying this color to relevant UI elements like buttons, links, or headers.

# Inject custom CSS using the theme configuration
custom_css = f"""
<style>
/* Container styling */
.reportview-container {{
    background-color: {THEME_CONFIG['backgroundColor']};
    color: {THEME_CONFIG['textColor']};
    font-family: {THEME_CONFIG['font']};
}}

/* Sidebar styling */
.sidebar .sidebar-content {{
    background: {THEME_CONFIG['secondaryBackgroundColor']};
}}

+ /* Button and link styling */
+ .stButton>button, .stTextInput>div>div>input:focus, a {{
+     color: {THEME_CONFIG['primaryColor']};
+ }}
+ 
+ /* Header styling */
+ h1, h2, h3, h4, h5, h6 {{
+     color: {THEME_CONFIG['primaryColor']};
+ }}
</style>
"""
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
# Inject custom CSS using the theme configuration
custom_css = f"""
<style>
/* Container styling */
.reportview-container {{
background-color: {THEME_CONFIG['backgroundColor']};
color: {THEME_CONFIG['textColor']};
font-family: {THEME_CONFIG['font']};
}}
/* Sidebar styling */
.sidebar .sidebar-content {{
background: {THEME_CONFIG['secondaryBackgroundColor']};
}}
</style>
"""
# Inject custom CSS using the theme configuration
custom_css = f"""
<style>
/* Container styling */
.reportview-container {{
background-color: {THEME_CONFIG['backgroundColor']};
color: {THEME_CONFIG['textColor']};
font-family: {THEME_CONFIG['font']};
}}
/* Sidebar styling */
.sidebar .sidebar-content {{
background: {THEME_CONFIG['secondaryBackgroundColor']};
}}
/* Button and link styling */
.stButton>button, .stTextInput>div>div>input:focus, a {{
color: {THEME_CONFIG['primaryColor']};
}}
/* Header styling */
h1, h2, h3, h4, h5, h6 {{
color: {THEME_CONFIG['primaryColor']};
}}
</style>
"""

@t0mdavid-m
Copy link
Member

Could you describe how your PR differs from #172 and why the CSS injection is needed?

@onemriganka
Copy link
Author

In contrast to #172—which primarily focused on enhancing visualization themes (i.e., aligning plot themes across libraries and improving responsiveness)—this PR introduces a modular, configuration-driven theming system. It adds a dedicated configuration file (config.py) that reads theme settings (colors, fonts, etc.) from environment variables and then uses CSS injection in app.py to override Streamlit’s default styling.

The CSS injection is needed because Streamlit’s built-in theming (via config.toml) is somewhat limited when it comes to granular UI customizations. By dynamically generating a custom CSS block from the external configuration, we can ensure consistent and immediate application of user-defined styles (for both containers and sidebars) without modifying core code. This approach also maintains backward compatibility—if no environment variables are provided, default styling is preserved.

@t0mdavid-m
Copy link
Member

Thanks for the suggestion. However, I dont think functionality to set themes via environment variables is needed.

@t0mdavid-m t0mdavid-m closed this Mar 29, 2025
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.

[STILL AVAILABILITY - multi-task issue] Additional Tests

2 participants