avatarHelped by a Nerd

Summary

This article provides guidance on implementing dark mode in a Dash application using Dash Mantine Components.

Abstract

The article titled "How To Create a Dash Application With Dark Mode" introduces the concept of dark mode, which is increasingly popular across various applications and operating systems. It specifically focuses on integrating dark mode into a Dash application using Dash Mantine Components. The author illustrates the visual impact of dark mode in a Dash application with an accompanying image. The complete code for the implementation is available on GitHub, and the article walks through the necessary steps to set up the application layout and callback function for toggling between light and dark themes. The author emphasizes the use of the MantineProvider component to apply a theme across the application and demonstrates how to update the theme dynamically based on user interaction with a toggle switch. Additionally, the article encourages readers to explore the author's other work on enhancing Dash applications with features like skeleton loading.

Opinions

  • The author believes that dark mode is an essential feature for modern applications.
  • They suggest that adding dark mode to a Dash application is straightforward when using Dash Mantine Components.
  • The author values brevity and clarity, providing a concise example of implementing the dark mode feature.
  • They endorse the practice of using version control systems like GitHub to share and collaborate on code.
  • The author encourages continuous learning and improvement by inviting readers to check out their latest article on skeleton loading in Dash applications.
  • They express gratitude to the readers and actively promote subscribing to their content for further engagement and learning opportunities.

How To Create a Dash Application With Dark Mode

Dark mode is becoming more and more popular, and many applications and operating systems now offer a dark mode option. In this article, you will learn how to add easily dark mode to your Dash application with Dash Mantine Components.

In the following picture, you can see what the dark mode setting looks like in the Dash application. To keep it short and sweet, we will be looking at the app.layout and callback for switching to dark mode.

Image by author

The Code Can Be Found on GitHub

The complete code is available on GitHub. If you want to reproduce it, you need to install all dependencies that are listed in the requirements file.

If you want to know how to add skeleton loading to your Dash app, then feel free to check out my latest article: Upgrade Your Dash Application With Skeleton Loading + Code.

Layout

To apply a dark color theme, you have to wrap your application in MantineProvider and specify the colorScheme.

app.layout = dmc.MantineProvider(
 id="app-theme",
    theme={
  "colorScheme": "white",
    },
    inherit=True,
    withGlobalStyles=True,
    withNormalizeCSS=True,
    children=["Your Layout"]
)

the dmc.MantineProvider component enables us to apply a theme to the entire application. It is mandatory to set an id for the MantineProvider component as well as the theme argument. The current layout will have a white color scheme and will apply global CSS styles and normalization to the app. The complete user interface of your application needs to be in the children argument. Take your time and have a look at the sample layout here.

Callback

The following code set up a callback function for the toggle switch in the header of our application. The first argument theme is required to be able to update the theme dictionary. It contains the whole theme dictionary, which we defined in the app.layout. It would be also possible to return the whole dictionary. However, the code becomes long if you set multiple theme properties. Therefore, retrieving the theme and updating it is the better way. Depending on if the switch is toggled, we return the updated dictionary with the colorScheme dark, or white.

@app.callback(
    Output('app-theme', 'theme'),
    Input('app-theme', 'theme'),
    Input("switch-theme", "checked"),
    prevent_intial_call=True)
def switch_theme(theme, checked):
    if not checked:
        theme.update({'colorScheme': 'dark'})
    else:
        theme.update({'colorScheme': 'white'})
    return theme

Thanks for reading! Before you go, consider subscribing to my content and get all my articles in your inbox. You can subscribe here! You can also sign up for my newsletter to get extra free content delivered right to your inbox.

Dark Mode
Dash
Python
Dash Mantine Components
Plotly Dash
Recommended from ReadMedium