Lightweight Auxiliary Framework for Writing Object-Oriented Dash Code.
Project description
# Dash Building Blocks
## Getting Started
#### examples/clones.py
The following example contains a Clone class that encapsulates the layout and callback of a "clone", inheriting the base class Block.
This allows the user to easily create as many "clones" as desired and resolve the many callbacks without having to wrestle with the long and mandatorily unique Dash component ids. The Block class does the dirty work behind the scenes, mapping the registered ids to the respective global Dash component ids.
~~~python
import dash
from dash.dependencies import Output, Input, State
import dash_html_components as html
import dash_core_components as dcc
import dash_building_blocks as dbb
N_CLONES = 10
class Clone(dbb.Block):
def layout(self):
return html.Div([
html.Div('I am a clone.', self.register('div')),
html.Button('Click Me!', self.register('button'))
])
def callbacks(self, state_n_clones):
@self.app.callback(
self.output('div', 'children'),
[self.input('button', 'n_clicks')],
[self.state('div', 'children'),
state_n_clones]
)
def update_clone_message(n_clicks, children, n_clones):
if children == 'I am a clone.':
return 'There are {} of us.'.format(n_clones)
else:
return 'I am a clone.'
app = dash.Dash()
clones = [Clone(app, id=str(i)) for i in range(N_CLONES)]
layout = html.Div(
children=[
html.Div(N_CLONES,
id='how-many-clones',
style={'display': 'none'})
] + [
clone.layout for clone in clones
]
)
app.layout = layout
for clone in clones:
clone.callbacks(State('how-many-clones', 'children'))
if __name__ == '__main__':
app.run_server(debug=True, port=5000, host='0.0.0.0')
~~~
#### examples/location.py
~~~python
import dash
import dash_html_components as html
import dash_core_components as dcc
import dash_building_blocks as dbb
import json
EMPTY_MAP = {
'data': [{
'lon': [],
'lat': [],
'type': 'scattergeo'
}],
'layout': {}
}
class Map(dbb.Block):
def layout(self):
return dcc.Graph(
figure=EMPTY_MAP,
id=self.register('map')
)
def callbacks(self, input_location):
@self.app.callback(
self.output('map', 'figure'),
[input_location]
)
def update_map(location):
location = json.loads(location)
lon = location['longitude']
lat = location['latitude']
data = [dict(
type = 'scattergeo',
lon = [lon],
lat = [lat],
text = 'Here!',
mode = 'markers',
marker = dict(
size = 8,
opacity = 0.8,
symbol = 'x',
line = dict(
width=1,
color='rgba(102, 102, 102)'
),
color = 'red',
)
)]
layout = dict(title='World Map')
return dict(data=data, layout=layout)
app = dash.Dash()
app.config.suppress_callback_exceptions = True
store = dbb.Store(app, hide=True)
map = Map(app)
userinput = dbb.InputForm(app,
inputs=['longitude', 'latitude'],
form_id=store.register('user-input'))
layout = html.Div(
children=[map.layout, userinput.layout, store.layout]
)
app.layout = layout
map.callbacks(store.input('user-input'))
if __name__ == '__main__':
app.run_server(debug=True, port=5000, host='0.0.0.0')
~~~
## Getting Started
#### examples/clones.py
The following example contains a Clone class that encapsulates the layout and callback of a "clone", inheriting the base class Block.
This allows the user to easily create as many "clones" as desired and resolve the many callbacks without having to wrestle with the long and mandatorily unique Dash component ids. The Block class does the dirty work behind the scenes, mapping the registered ids to the respective global Dash component ids.
~~~python
import dash
from dash.dependencies import Output, Input, State
import dash_html_components as html
import dash_core_components as dcc
import dash_building_blocks as dbb
N_CLONES = 10
class Clone(dbb.Block):
def layout(self):
return html.Div([
html.Div('I am a clone.', self.register('div')),
html.Button('Click Me!', self.register('button'))
])
def callbacks(self, state_n_clones):
@self.app.callback(
self.output('div', 'children'),
[self.input('button', 'n_clicks')],
[self.state('div', 'children'),
state_n_clones]
)
def update_clone_message(n_clicks, children, n_clones):
if children == 'I am a clone.':
return 'There are {} of us.'.format(n_clones)
else:
return 'I am a clone.'
app = dash.Dash()
clones = [Clone(app, id=str(i)) for i in range(N_CLONES)]
layout = html.Div(
children=[
html.Div(N_CLONES,
id='how-many-clones',
style={'display': 'none'})
] + [
clone.layout for clone in clones
]
)
app.layout = layout
for clone in clones:
clone.callbacks(State('how-many-clones', 'children'))
if __name__ == '__main__':
app.run_server(debug=True, port=5000, host='0.0.0.0')
~~~
#### examples/location.py
~~~python
import dash
import dash_html_components as html
import dash_core_components as dcc
import dash_building_blocks as dbb
import json
EMPTY_MAP = {
'data': [{
'lon': [],
'lat': [],
'type': 'scattergeo'
}],
'layout': {}
}
class Map(dbb.Block):
def layout(self):
return dcc.Graph(
figure=EMPTY_MAP,
id=self.register('map')
)
def callbacks(self, input_location):
@self.app.callback(
self.output('map', 'figure'),
[input_location]
)
def update_map(location):
location = json.loads(location)
lon = location['longitude']
lat = location['latitude']
data = [dict(
type = 'scattergeo',
lon = [lon],
lat = [lat],
text = 'Here!',
mode = 'markers',
marker = dict(
size = 8,
opacity = 0.8,
symbol = 'x',
line = dict(
width=1,
color='rgba(102, 102, 102)'
),
color = 'red',
)
)]
layout = dict(title='World Map')
return dict(data=data, layout=layout)
app = dash.Dash()
app.config.suppress_callback_exceptions = True
store = dbb.Store(app, hide=True)
map = Map(app)
userinput = dbb.InputForm(app,
inputs=['longitude', 'latitude'],
form_id=store.register('user-input'))
layout = html.Div(
children=[map.layout, userinput.layout, store.layout]
)
app.layout = layout
map.callbacks(store.input('user-input'))
if __name__ == '__main__':
app.run_server(debug=True, port=5000, host='0.0.0.0')
~~~
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
Built Distribution
Close
Hashes for dash_building_blocks-0.0.2.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 771036594f68518b71e46e707585484bc3de5d9c0d5075630b784009759d0e47 |
|
MD5 | 42fbab42045f12d326ee81f15883ff56 |
|
BLAKE2b-256 | 028e10dbd385ef2060d5c963c7e1f4d35243f6d259f2e3cdea52477893a117ad |
Close
Hashes for dash_building_blocks-0.0.2-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 12bf7ad32ad1d7b714afcabff17f87ae69d501d4266fa22aa68e317ebac64986 |
|
MD5 | 5221a549d729b30145d643fbbaabe596 |
|
BLAKE2b-256 | e3cd28a05a2c25b3b5e840077b3f8a8de4a05ac0c9d8df8a46c1609407fa7cfb |