Skip to content

stpnvkirill/dash-manager

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

20 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Dash Manager

Dash Manager is an easy-to-use device that allows you to combine multiple Dash applications on a single Flask server. A layout wrapper is also adding for each application into which common elements, such as the navigation bar, can be encoded.

Simple Example

from dash_manager import DashManager, BaseView
from dash import Dash, html


manager = DashManager(template_mode='bootstrap') # or mantine


# Pages
page = Dash(name='First Dashboard')
page.layout = lambda: html.Div('This is First Dashboard')

page2 = Dash(name='Second Dashboard', url_base_pathname='/two/')
page2.layout = lambda: html.Div('This is Second Dashboard')

page3 = Dash(name='Third Dashboard', url_base_pathname='/three/')
page3.layout = lambda: html.Div('This is Third Dashboard')


# Views
manager.add_view(BaseView(page, category='Dropdown'))
manager.add_view(BaseView(page2, category='Dropdown'))
manager.add_view(BaseView(page3))

# Run app
manager.run(debug=True)

Instalation

Dash-manager is published as a Python package and can be installed with pip, ideally by using a virtual environment. Open up a terminal and install dash-manager with:

pip install dash-manager

This will automatically install compatible versions of all dependencies: Flask, Dash, Dash Bootstrap & Dash Mantine. Dash manager always strives to support the latest versions, so there's no need to install those packages separately.

Creating your app

After you've installed Dash Manager, you can create a recommended application structure. Go to the directory where you want your project to be located and enter:

dash-manager new .

This will create the following structure, where in each file there will be detailed descriptions:

.
├─ assets/
├─ webApp/
│  └─ views
│     └─ first_dash
│        └─ __init__.py
│        └─ app.py
│     └─ second_dash
│        └─ __init__.py
│        └─ app.py
│     └─ __init__.py
│  └─ __init__.py
│  └─ config.py
│  └─ server.py
└─ wsgi.py

The resulting project is a basic application that can be run via wsgi.py:

python wsgi.py

Protecting

Dash Manager lets you define access control rules on each of your view classes by simply overriding the is_accessible method. How you implement the logic is up to you, but if you were to use a low-level library like Flask-Login, then restricting access could be as simple as:

from dash_manager import BaseView
from flask import redirect, url_for
from flask_login import current_user


class MyView(BaseView):

    def is_accessible(self):
        return current_user and current_user.is_authenticated

    def inaccessible_callback(self, name, **kwargs):
        # redirect to login page if user doesn't have access
        return redirect(url_for('login', next=request.url))

In the navigation menu, components that are not accessible to a particular user will not be displayed for that user. The main drawback is that you still need to implement all of the relevant login, registration, and account management views yourself.

Customization

You can use three built-in themes for your application: Bootstrap, Mantine & Clear. But if you need a completely custom interface: override dash_manager.manager.template.BaseTemplate class.

class MyTemplate(BaseTemplate):
    def navbar(self, menu: list, links: list):
        """
            Override this method to create a custom navigation bar

            Menu is list of dash_manager.manager.menu.MenuView or 
            dash_manager.manager.menu.MenuCategory object
        """
        ...

    def app_container(self, navbar, app_layout, footer):
        """
            Override this method to create a custom container
        """
        ...

    def footer(self):
        """
            Override this method to create a custom footer
        """
        ...

Read more about this here

Grouping Views

When adding a view, specify a value for the category parameter to group related views together in the menu:

manager.add_view(BaseView(page, category='Dropdown'))
manager.add_view(BaseView(page2, category='Dropdown'))

This will create a top-level menu item named ‘Dropdown’, and a drop-down containing links to the two views.

And to add arbitrary buttons to the menu:

btn = dmc.Anchor(
    dmc.ActionIcon(
        DashIconify(
            icon='radix-icons:avatar', width=22
        ),
        variant="outline",
        radius='md',
        size=36,
        color='indigo',
    ), 
    href='/profile', 
    refresh=True)


manager.add_link(btn)

About

A tool for combining Dash apps on a single server

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages