Plotly Dash

repository·dev·Indexed 12 days ago

https://github.com/plotly/dash

A Python framework for building interactive web applications for data science and machine learning. Built on Plotly.js, React, and Flask, Dash allows developers to create declarative and reactive UIs with components like DataTable, dash-core-components (dcc), and dash-html-components using only Python code.

Tokens
23.7K
Snippets
64
Records
103
Agent score
98%

What's inside Dash

  1. Overview of Dash DataTable

    dev

    Dash DataTable is an interactive React.js and TypeScript-based component designed for viewing, editing, and exploring large datasets within Dash applications.

    Key characteristics:

    • Markup: Rendered using standard, semantic HTML <table> markup for accessibility and responsiveness.
    • Customization: Highly customizable through its properties to support complex, spreadsheet-driven applications.
    • Browser Support: Supported in Chrome, Firefox, Safari, Edge (version 15+), and Internet Explorer 11.
  2. Overview of Dash

    dev

    Dash is a Python framework designed for building machine learning and data science web applications. It allows developers to tie modern UI elements (such as dropdowns, sliders, and graphs) directly to analytical Python code.

    Dash is built on top of three core technologies:

    • Plotly.js: For high-quality interactive charting (supporting ~50 chart types, including maps).
    • React: For building the user interface.
    • Flask: For the underlying web server.

    Dash applications are declarative and reactive, enabling the creation of complex apps with multiple interactive elements and cross-filtering capabilities using only Python code.

  3. Conceptual model of Dash App Objects

    dev

    The standard Dash workflow follows a specific lifecycle that any new back end should replicate:

    1. Initialization: Create the app object (e.g., app = Dash(...)).
    2. Layout Definition: Assign a nested structure of components to the app's layout property (e.g., app.layout = html.Div(...)).
    3. Callback Registration: Define logic that connects inputs (like button clicks) to outputs (like graph updates) using decorators or specific methods.
    4. Server Execution: Run the web server (e.g., app.run_server(...)).
    from dash import Dash, html
    
    # 1. Initialization
    app = Dash(__name__)
    
    # 2. Layout Definition
    app.layout = html.Div('Hello Dash')
    
    # 3. Callback Registration
    @app.callback(
        Output('output-id', 'children'),
        Input('input-id', 'value')
    )
    def update_output(input_value):
        return f'Input value is: {input_value}'
    
    # 4. Server Execution
    if __name__ == '__main__':
        app.run_server(debug=True)
  4. Compare Dash Open Source and Dash Enterprise

    dev

    Dash is available in two primary versions depending on your deployment and scaling needs:

    Dash Open Source

    • Best for running apps on a local laptop or workstation.
    • Ideal for individual development and prototyping.
    • Does not include built-in tools for easy organizational access or enterprise-grade scaling.

    Dash Enterprise

    Designed for department or company-wide consumption, offering advanced features for scaling and productivity:

    ML Ops & Scaling:

    • App Manager: Point-and-click deployment and management without requiring IT/DevOps.
    • Kubernetes scaling: Horizontal scaling and high availability via Kubernetes architecture.
    • No code auth: Built-in support for LDAP, AD, PKI, Okta, SAML, OpenID Connect, OAuth, SSO, and email authentication.
    • Job Queue: Enables asynchronous background processing for heavy computations, moving them out of synchronous Dash callbacks.

    Low-Code Productivity:

    • Design Kit: UI styling and branding without writing CSS.
    • Snapshot Engine: Save/share app views as links or PDFs, or automate email reports.
    • Dashboard Toolkit: Drag-and-drop layouts and chart editing.
    • Embedding: Native embedding of Dash apps into existing websites without using IFrames.

    Enterprise AI & Big Data:

    • AI App Marketplace: Pre-built templates for common AI/ML business problems.
    • Big Data Integration: Connects to Dask, Databricks, NVIDIA RAPIDS, Snowflake, Postgres, Vaex, and more.
    • GPU & Dask Acceleration: Support for GPU and parallel CPU computing.
    • Data Science Workspaces: Onboard code editors for Python, R, and Julia.
  5. Install dash-html-components dependencies

    dev

    To set up the development environment for dash-html-components, follow these steps:

    1. Python Environment: Create and activate a virtual environment.
      • Unix/macOS: source venv/bin/activate
      • Windows: venv\Scripts\activate
    2. Python Dependencies: Install the required build packages using pip install -r dev-requirements.txt.
    3. NPM Dependencies: Install the necessary JavaScript packages using npm ci.
    $ virtualenv venv
    $ venv/bin/activate
    $ pip install -r dev-requirements.txt
    $ npm ci
  6. Develop and test dash-html-components in Dash

    dev

    To develop and test changes to the components within a Dash application, use the following workflow:

    1. Watch for changes: Start the watcher to rebuild the JS bundle on file changes.
      npm run build:watch
    2. **Install locally**: Generate metadata and build the JavaScript bundle.
       ```bash
    npm run install-local
    python setup.py install
    1. Run your app: Import dash_html_components into your Dash layout and run your Python script.
      python my_dash_layout.py
    
    ```bash
    # 1. Watch for changes
    $ npm run build:watch
    
    # 2. Install module locally
    $ npm run install-local
    $ python setup.py install
    
    # 3. Run the Dash layout
    $ python my_dash_layout.py
  7. Create a production build and publish

    dev

    To prepare a production release for both NPM and PyPI, follow these steps:

    1. Build JS: Run npm run build.
    2. Create Tarball: Run python setup.py sdist to generate a distribution tarball in the dist/ folder.
    3. Verify: Install the generated tarball in a clean environment using pip install <tarball-name>.tar.gz.
    4. Publish to PyPI: Use twine upload dist/*.
    5. Publish to NPM: Use npm publish.

    Important Note on CDN usage: Publishing to NPM makes the JavaScript bundles available on the unpkg CDN. By default, Dash servers components from the remote unpkg CDN. If you do not publish to NPM, you must set the serve_locally flag to True in your Dash application to ensure the components load correctly.

    # 1. Build code
    $ npm run build
    
    # 2. Create Python tarball
    $ python setup.py sdist
    
    # 3. Test tarball (in a new env)
    $ pip install dash-html-components-<version>.tar.gz
    
    # 4. Publish to PyPI
    $ twine upload dist/*
    
    # 5. Publish to NPM
    $ npm publish
  8. Set up Dash Core Components for development

    dev

    To develop dash-core-components (dcc) within the main dash repository, you must install the repository in development mode with the necessary extras and run the build process.

    Prerequisites:

    • Python 3 (required as of Dash 2.0)
    • A virtual environment (recommended)

    Steps:

    1. Create and activate a virtual environment.
    2. Install dash with dev and testing dependencies in editable mode.
    3. Install NPM dependencies and run the build process to compile all Dash packages, including dcc.
    4. Install the dcc package itself in editable mode.
    # Create and activate virtualenv
    $ python -m venv venv && . venv/bin/activate
    
    # Install dash with dev and testing dependencies
    $ pip install -e .[dev,testing]
    
    # Build all of dash, including dcc
    $ npm ci && npm run build
    
    # Install dcc in editable mode
    $ pip install -e .