Py.Cafe

nataliatsyporkin/

dash-color-scales-swatches

Plotly Built-In Colorscale Viewer using Dash

DocsPricing
  • app.py
  • help_function.py
  • requirements.txt
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
from dash import Dash, Input, Output, callback, dcc, html
import dash
import dash_bootstrap_components as dbc
import plotly.express as px
import plotly.graph_objects as go
import plotly.colors as colors
from help_function import create_polar_subplots


app = Dash(__name__, external_stylesheets=[dbc.themes.SPACELAB, dbc.icons.FONT_AWESOME])


# Define the layout options
layout_params = {'margin': dict(l=70, t=50, r=20), 'paper_bgcolor':'#E5ECF6', 
                 'modebar_orientation': 'v', 
                 'xaxis_zeroline': False, 'font_size':14}

# Define config mode for plotly graph
config_mode = {'displaylogo': True, 
               'modeBarButtonsToRemove': ['zoom', 'pan', 'select', 'zoomIn', 'zoomOut', 'lasso', 'autoScale']}  

#------------------------------------------------------------------------
palette_names = [i['name'] for i in px.colors.cyclical.swatches_cyclical().data][:-1]
polar = create_polar_subplots(palette_names)

# Define a functionn to generate figure and update layout 
def create_figure(swatches, swatch_type): 
    n_swatches = len(swatches.data) 
    fig = swatches.update_traces(width=0.7) 
    fig.update_layout( 
        **layout_params, 
        height=50 + 40 * n_swatches, 
        title=f'{swatch_type} Swatches ({n_swatches} colorscales)' 
    ) 
    return fig 

# Generate figures 
fig_seq = create_figure(px.colors.sequential.swatches(), 'Sequential') 
fig_div = create_figure(px.colors.diverging.swatches(), 'Diverging') 
fig_quel = create_figure(px.colors.qualitative.swatches(), 'Qualitative') 
fig_cycl = create_figure(px.colors.cyclical.swatches(), 'Cyclical')     

#----------------------------------------------------------------------

# Create markdown context for info popover
popover_info = dcc.Markdown('''
**Do you want to know how to reverse any colorscale in Plotly?**   

---
Here is the secret: 

- All the Plotly colorscales presented here can be reversed by simply adding **'_r'** at the end of their name
(for example: 'BrBG_r', 'Plotly_r', 'Turbo_r', etc.).
  
Enjoy!πŸ‘Œ''', style={'background-color': '#E5ECF6', 'color': '#2fa4e7'})

# Create info button with popover 
btn_pop = html.Div([
        dbc.Button(
            html.I(className='fa-solid fa-circle-info mx-1', style={'color': '#2fa4e7'}),
            id='info',
            color='btn-outline-info',
            style={ 'background-color': '#E5ECF6', 'margin-top':'50px'}, # Button background color                    
            n_clicks=0),
        dbc.Popover(
            children=popover_info,
            target='info',  
            body=True, 
            placement="bottom", 
            trigger='hover', 
            style={ 'background-color': '#E5ECF6', 
                    'width': '260px', 
                    'justify-items': 'center'}),             
    ]) 

# Create source button with popover 
btn_source = html.Div([
        dbc.Button(
            html.I(className='fa-solid fa-database mx-1', style={'color': '#2fa4e7'}),
            id='source',
            color='btn-outline-info',
            style={ 'background-color': '#E5ECF6'}, # Button background color                    
            n_clicks=0),
        dbc.Popover([
            dbc.PopoverHeader(
                html.H6("Built-In Colorscales in Plotly"), style={ 'background-color': '#E5ECF6'}),
            dbc.PopoverBody([                
                html.P(html.A('Continuous Color Scales' ,
                        href="https://plotly.com/python/colorscales/#continuous-vs-discrete-color", target="_blank")),
                html.A('Discrete Color Scales',
                        href="https://plotly.com/python/discrete-color", target="_blank"),                            
                ]),
            ],
            target='source',  
            body=True, 
            placement="bottom", 
            trigger='hover', 
            style={ 'background-color': '#E5ECF6',                   
                    'width': '300px', 
                    'justify-items': 'center'})
        ])                              
  

# Create app layout=================================================================

app.layout = dbc.Container([
    dbc.Row([
        dbc.Col([
            dbc.Card([ 
                dbc.CardBody([ 
                    html.H2('🌈', style={'text-align': 'center', 'margin': '0 10px 0 0'}), 
                    html.H2( "Color Scales Swatches", className='text-center mb-0 py-1', 
                        style={ 'font-weight': '600', 
                                'background': 'linear-gradient(to right, yellow, green, cyan, blue, violet)', 
                                'font-size': '32px', 
                                'color': 'transparent', 
                                'background-clip': 'text', 
                                '-webkit-background-clip': 'text' } )
                ], style={'display': 'flex', 'align-items': 'center', 'justify-content':'center', 'padding':'5px'}),
            ], style={'background': '#E5ECF6', 'align-content': 'center'}, className="mb-3")
        ], width=11)
    ]),
    dbc.Row([
        dbc.Col(
            dbc.Card(dcc.Graph(figure=fig_seq, config=config_mode), body=True), width=5),
        dbc.Col([btn_source, btn_pop, 
            ], width=1, style={'justify-items':'center'}),    
        dbc.Col([
            dbc.Card(dcc.Graph(figure=fig_div, config=config_mode), body=True),
            dbc.Card(dcc.Graph(figure=fig_quel, config=config_mode), body=True, class_name='my-3'),
            dbc.Card(dcc.Graph(figure=fig_cycl, config=config_mode), body=True),
            dbc.Card(dcc.Graph(figure=polar, config=config_mode), body=True, class_name='mt-3')
            ] , width=5),
        ])   
    ])


if __name__ == "__main__":
    app.run_server(debug=False)