Skip to main content

Detect dark mode in Jupyter environments

Project description

jupyter-dark-detect

Detect dark mode in Jupyter environments (Notebook, Lab, VS Code, etc.).

Installation

pip install jupyter-dark-detect

Usage

from jupyter_dark_detect import is_dark

# Check if Jupyter is running in dark mode
if is_dark():
    print("Dark mode is enabled!")
    # Use dark theme colors for visualizations
    bg_color = "#1e1e1e"
    text_color = "#d4d4d4"
else:
    print("Light mode is enabled!")
    # Use light theme colors for visualizations
    bg_color = "#ffffff"
    text_color = "#000000"

Features

  • Multiple detection strategies for maximum compatibility:

    • JupyterLab theme settings files
    • VS Code workspace and user settings
    • JavaScript-based DOM inspection
    • System preferences (macOS and Windows)
  • Zero configuration - just import and use

  • Lightweight with minimal dependencies (only requires IPython)

  • Cross-platform support for JupyterLab, Jupyter Notebook, VS Code, and more

How It Works

The package tries multiple detection methods in order:

  1. JupyterLab Settings: Checks ~/.jupyter/lab/user-settings/ for theme configuration
  2. VS Code Settings: When running in VS Code, checks both workspace and user settings
  3. JavaScript Detection: Uses IPython magic to inspect the DOM for theme classes
  4. System Preferences: Falls back to OS-level dark mode settings on macOS and Windows

Use Cases

  • Matplotlib/Plotly Visualizations: Automatically adjust plot colors based on theme
  • Rich Terminal Output: Style console output to match the notebook theme
  • Custom Widgets: Build theme-aware Jupyter widgets
  • Documentation: Generate screenshots that match the user's theme

Example: Matplotlib Integration

import matplotlib.pyplot as plt
from jupyter_dark_detect import is_dark

# Set style based on theme
plt.style.use('dark_background' if is_dark() else 'default')

# Your plotting code
plt.plot([1, 2, 3, 4], [1, 4, 9, 16])
plt.title("Theme-Aware Plot")
plt.show()

Example: Plotly Integration

import plotly.graph_objects as go
from jupyter_dark_detect import is_dark

# Create theme-aware Plotly figure
fig = go.Figure()
fig.add_trace(go.Scatter(x=[1, 2, 3, 4], y=[1, 4, 9, 16]))

# Update layout based on theme
if is_dark():
    fig.update_layout(
        template="plotly_dark",
        paper_bgcolor="#1e1e1e",
        plot_bgcolor="#1e1e1e"
    )
else:
    fig.update_layout(
        template="plotly_white",
        paper_bgcolor="white",
        plot_bgcolor="white"
    )

fig.show()

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

License

This project is licensed under the MIT License - see the LICENSE file for details.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

jupyter_dark_detect-0.1.0.tar.gz (7.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

jupyter_dark_detect-0.1.0-py3-none-any.whl (8.0 kB view details)

Uploaded Python 3

File details

Details for the file jupyter_dark_detect-0.1.0.tar.gz.

File metadata

  • Download URL: jupyter_dark_detect-0.1.0.tar.gz
  • Upload date:
  • Size: 7.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.4.17

File hashes

Hashes for jupyter_dark_detect-0.1.0.tar.gz
Algorithm Hash digest
SHA256 b0358da684f1e36d0a869e839fa55dc1d9183cfc3ff228fe416d5132a7f36bcc
MD5 ed740202511810a43504b68b45146b4b
BLAKE2b-256 725b4e3fc83ffd16d502bf34a143f1c6453da0ffacf7d461e825c5926bbcaa09

See more details on using hashes here.

File details

Details for the file jupyter_dark_detect-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for jupyter_dark_detect-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 198a7fa1270c6ce1ac2fd056cea9300e018bb8d04211de4c946cbe9e03e7a89d
MD5 dbfc6ea76931f1db726b15c81f7a6513
BLAKE2b-256 76c08cc378aad5ccfcc37adeda27d3864f20ea808ad5b63572f5b986a42a649c

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page