I’d like to show to the world the library I’m working on and gather feedback. The main point of the library is to allow user to bind the layout to data directly, thus avoiding need for callbacks altogether.
Let’s start with some example. We’ll create an app with single
Input component, that renders back whatever the user has typed in.
from simpledash.callbacks import setup_callbacks app.layout = html.Div([ dash_core_components.Input(id='data-input', className='row'), # with Simple Dash you can use a dash dependency object directly in the layout # it will be replaced by actual `data-input.value` and updated every time it changes html.Div(dash.dependencies.Input('data-input', 'value'), className='row', id='output-div') ]) setup_callbacks(app) # this will scan the layout and create all necessary callback functions
and that’s it! No callbacks needed, the library has taken care of everything.
This was obviously extremely simple, but with simpledash you’re able to define much more complex operations than this, so let’s see another example. We would like to have two Inputs (named A and B) and let user decide which one is going to be used for rendering the output value. This choice will be done via dropdown.
import dash_core_components as dcc from dash.dependencies import Input from simpledash.data.data_providers import data_provider input_a = dcc.Input(id='data-input-a', value="") input_b = dcc.Input(id='data-input-b', value="") input_chooser = dcc.Dropdown(id='input-chooser', options=options_from(['A', 'B'])) # use a `data_provider` annotation to indicate, that the method provides data based on inputs # inputs used by function are declared as arguments to the decorator # and are later on passed to the function as arguments @data_provider(Input('data-input-a', 'value'), Input('data-input-b', 'value'), Input('input-chooser', 'value')) def output_value(input_a_value, input_b_value, input_chooser_value): if input_chooser_value == "A": return input_a_value if input_chooser_value == "B": return input_b_value return "" app.layout = html.Div([ html.Div(["A: ", input_a], className='row'), html.Div(["B: ", input_b], className='row'), html.Div(["Which one to show? ", input_chooser], className='row'), html.Br(), # you simply use `data_provider` instance in the layout (as in previous example) html.Div(["Here's the output: ", output_value], className='row', id='output'), # but you can also run some simple operations on `data_provider`, like `upper()` html.Div(["Also in uppercase!: ", output_value.upper()], className='row', id='output-upper') ]) setup_callbacks(app)
This time we have used
data_provider decorator to declare the function that is able to provide data based on inputs. Note, that this is a plain python function, so you should be able to do any operation on inputs, regardless of the complexity.
Interesting thing we see in the example is
output_value.upper(). This is just a syntax sugar
that Simple Dash gives you - instead of writing another
data_provider to do the uppercasing,
we can call the method directly on
output_value (and this will create new
data_provider under the hood for you).
Please note that the set of operations you are able to do on
data_provider instance are limited to:
- accessing the property (
- accessing the item by index (
- calling the method (
For even more complex operations, you are able to nest
data_providers, but to see an example let me redirect you to the README.
This is the very first release of the library, so mostly I’m interested in validation of the idea. What do you think of it? Would you be keen to use this kind of API vs callbacks?
I’ve been user-testing the lib on some people in my company and the initial feedback is very positive - people find it more intuitive to build an app with simpledash rather than plain dash (the caveat here is those people didn’t have much experience with dash previously, so maybe that’s why callbacks sounded strange to them. I wonder what power-user of dash would think - please, help me out).
Anyways, here’s the link to the repo: https://github.com/rtshadow/simple-dash
I’m very keen to hear your feedback.
Thanks a lot,