This is the 1st chapter of the Dash Fundamentals.
The next chapter covers Dash callbacks.

using Dash

app = dash()

app.layout = html_div() do
    html_h1("Hello Dash"),
    html_div("Dash: A web application framework for your data."),
    dcc_graph(
        id = "example-graph-1",
        figure = (
            data = [
                (x = ["giraffes", "orangutans", "monkeys"], y = [20, 14, 23], type = "bar", name = "SF"),
                (x = ["giraffes", "orangutans", "monkeys"], y = [12, 18, 29], type = "bar", name = "Montreal"),
            ],
            layout = (title = "Dash Data Visualization", barmode="group")
        )
    )
end

run_server(app, "0.0.0.0", debug=true)

Hello Dash

Dash: A web application framework for your data.
$ julia app.jl
[ Info: Listening on: 0.0.0.0:8050

Visit http://127.0.0.1:8050/
in your web browser. You should see an app that looks like the one above.

Note:

  1. The layout is composed of a tree of “components” such as
    html_div
    and dcc_graph.
  2. The DashHtmlComponents
    function
    has a component for every HTML tag.
    The html_h1("Hello Dash")
    component generates a <h1>Hello Dash<h1> HTML element in your app.
  3. Not all components are pure HTML.
    DashCoreComponents
    describe higher-level components that are interactive and are generated with
    JavaScript, HTML, and CSS through the React.js library.

  4. Each component is described entirely through keyword attributes.
    Dash is declarative: you will primarily describe your app
    through these attributes.

  5. The children property is special. By convention, it’s always the first attribute which means that you can omit it: html_h1(children="Hello Dash") is the same as html_h1("Hello Dash"). It can contain a string, a number, a single component, or a list of components.
  6. The fonts in your app will look a little bit different than what is displayed here.
    This app is using a custom CSS stylesheet and Dash Enterprise Design Kit
    to modify the default styles of the elements. You can learn more about custom CSS in the
    CSS tutorial.

Making Your First Change

Dash includes “hot-reloading”. This feature is activated by default when
you run your app withrun_server(app, "0.0.0.0", debug=true).
This means that Dash
will automatically refresh your browser when you make a change in your code.

Give it a try: change the title “Hello Dash” in your app or change the
x or the y data. Your app should auto-refresh with your change.

Don’t like hot-reloading? You can turn this off withrun_server(app, "0.0.0.0", debug=true, dev_tools_hot_reload=false).
Learn more in Dash Dev Tools documentation
Questions? See the community forum hot reloading discussion.

More about HTML Components

DashHtmlComponents
contains a component class for every HTML tag as well as keyword arguments for all of the HTML arguments.

Let’s customize the text in our app by modifying the inline styles of the components.
Create a file named app.jl with the following code:

using Dash

app = dash()

app.layout = html_div(style = Dict("backgroundColor" => "#111111")) do
    html_h1(
        "Hello Dash",
        style = Dict("color" => "#7FDBFF", "textAlign" => "center"),
    ),
    html_div(
        "Dash: A web application framework for Julia",
        style = Dict("color" => "#7FDBFF"),
    ),
    dcc_graph(
        id = "example-graph-2",
        figure = (
            data = [
                (
                    x = ["giraffes", "orangutans", "monkeys"],
                    y = [20, 14, 23],
                    type = "bar",
                    name = "SF",
                ),
                (
                    x = ["giraffes", "orangutans", "monkeys"],
                    y = [12, 18, 29],
                    type = "bar",
                    name = "Montreal",
                ),
            ],
            layout = (
                title = "Dash Data Visualization",
                barmode = "group",
                plot_bgcolor = "#111111",
                paper_bgcolor = "#111111",
                font = Dict("color" => "#7FDBFF"),
            ),
        ),
    )
end

run_server(app, "0.0.0.0", debug=true)

Hello Dash

Dash: A web application framework for your data.

In this example, we modified the inline styles of the
html_div
and html_h1components with the style property.

html_h1(
    "Hello Dash",
    style = Dict("color" => "#7FDBFF", "textAlign" => "center"),
)

The above code is rendered in the Dash app as
<h1>Hello Dash<h1>.

There are a few important differences between the DashHtmlComponents
and the HTML attributes:

  1. The style property in HTML is a semicolon-separated string. In Dash,
    you can just supply a dictionary.
  2. The keys in the style dictionary are camelCased.
    So, instead of text-align, it’s textAlign.
  3. The HTML class attribute is className in Dash.
  4. The children of the HTML tag is specified through the children keyword
    argument. By convention, this is always the first argument and so it is often omitted.

Besides that, all of the available HTML attributes and tags are available
to you within your Julia context.


Reusable Components

By writing our markup in Julia, we can create complex reusable components like tables without switching contexts or languages.

Here’s a quick example that generates a Table from a
DataFrame. Create a file named app.jl with the following code:

using Dash
using DataFrames, CSV

csv_data = download("https://raw.githubusercontent.com/plotly/datasets/master/2011_us_ag_exports.csv")
df3 = CSV.read(csv_data, DataFrame)

function generate_table(dataframe, max_rows = 10)
    html_table([
        html_thead(html_tr([html_th(col) for col in names(df3)])),
        html_tbody([
            html_tr([html_td(dataframe[r, c]) for c in names(dataframe)]) for r = 1:min(nrow(dataframe), max_rows)
        ]),
    ])
end

app = dash()

app.layout = html_div() do
    html_h4("US Agriculture Exports (2011)"),
    generate_table(df3, 10)
end

run_server(app, "0.0.0.0", debug=true)

US Agriculture Exports (2011)

Unnamed: 0 state total exports beef pork poultry dairy fruits fresh fruits proc total fruits veggies fresh veggies proc total veggies corn wheat cotton
Alabama 1390.63 34.4 10.6 481.0 4.06 8.0 17.1 25.11 5.5 8.9 14.33 34.9 70.0 317.61
1 Alaska 13.31 0.2 0.1 0.19 0.6 1.0 1.56
2 Arizona 1463.17 71.3 17.9 105.48 19.3 41.0 60.27 147.5 239.4 386.91 7.3 48.7 423.95
3 Arkansas 3586.02 53.2 29.4 562.9 3.53 2.2 4.7 6.88 4.4 7.1 11.45 69.5 114.5 665.44
4 California 16472.88 228.7 11.1 225.4 929.95 2791.8 5944.6 8736.4 803.2 1303.5 2106.79 34.6 249.3 1064.95
5 Colorado 1851.33 261.4 66.0 14.0 71.94 5.7 12.2 17.99 45.1 73.2 118.27 183.2 400.5
6 Connecticut 259.62 1.1 0.1 6.9 9.49 4.2 8.9 13.1 4.3 6.9 11.16
7 Delaware 282.19 0.4 0.6 114.7 2.3 0.5 1.0 1.53 7.6 12.4 20.03 26.9 22.9
8 Florida 3764.09 42.6 0.9 56.9 66.31 438.2 933.1 1371.36 171.9 279.0 450.86 3.5 1.8 78.24
9 Georgia 2860.84 31.0 18.9 630.4 38.38 74.6 158.9 233.51 59.0 95.8 154.77 57.8 65.4 1154.07

More about Visualization

The DashCoreComponents
library includes a component called
dcc_graph.

dcc_graph
renders interactive data visualizations using the open source
plotly.js JavaScript graphing
library. Plotly.js supports over 35 chart types and renders charts in
both vector-quality SVG and high-performance WebGL.

The figure argument in thedcc_graph
component is the same figure argument that is used by plotly.py, Plotly’s
open source Julia graphing library.
Check out the plotly.py documentation and gallery
to learn more.

Here’s an example that creates a scatter plot from a
DataFrame. Create a file named app.jl with the following code:

using Dash
using DataFrames, CSV, PlotlyJS, RDatasets

iris = dataset("datasets", "iris")

p1 = Plot(iris, x=:SepalLength, y=:SepalWidth, mode="markers", marker_size=8, group=:Species)

app = dash()

app.layout = html_div() do
    html_h4("Iris Sepal Length vs Sepal Width"),
    dcc_graph(
        id = "example-graph-3",
        figure = p1,
    )
end

run_server(app, "0.0.0.0", debug=true)

These graphs are interactive and responsive.
Hover over points to see their values,
click on legend items to toggle traces,
click and drag to zoom,
hold down shift, and click and drag to pan.

Markdown

While Dash exposes HTML through DashHtmlComponents, it can be tedious to write your copy in HTML. For writing blocks of text, you can use the
dcc_markdown component in
DashCoreComponents. Create a file named app.jl with the following code:

using Dash

app = dash()
markdown_text = "
### Dash and Markdown

Dash apps can be written in Markdown.
Dash uses the [CommonMark](http://commonmark.org/)
specification of Markdown.
Check out their [60 Second Markdown Tutorial](http://commonmark.org/help/)
if this is your first introduction to Markdown!
"

app.layout = html_div() do
    dcc_markdown(markdown_text)
end

run_server(app, "0.0.0.0", debug=true)

Dash and Markdown

Dash apps can be written in Markdown.
Dash uses the CommonMark
specification of Markdown.
Check out their 60 Second Markdown Tutorial
if this is your first introduction to Markdown!

Core Components

DashCoreComponents
includes a set of higher-level components like dropdowns, graphs, markdown blocks, and more.

Like all Dash components, they are described entirely declaratively.
Every option that is configurable is available as a keyword argument
of the component.

We’ll see many of these components throughout the tutorial.
You can view all of the available components in the
Dash Core Components overview.

Here are a few of the available components.
Create a file named app.jl with the following code:

using Dash

app = dash()

dropdown_options = [
    Dict("label" => "New York City", "value" => "NYC"),
    Dict("label" => "Montreal", "value" => "MTL"),
    Dict("label" => "San Francisco", "value" => "SF"),
]
app.layout = html_div(style = Dict("columnCount" => 2)) do
    html_label("Dropdown"),
    dcc_dropdown(options = dropdown_options, value = "MTL"),
    html_label("Multi-Select Dropdown"),
    dcc_dropdown(
        options = dropdown_options,
        value = ["MTL", "SF"],
        multi = true,
    ),
    html_label("Radio Items"),
    dcc_radioitems(options = dropdown_options, value = "MTL"),
    html_label("Checkboxes"),
    dcc_checklist(options = dropdown_options, value = ["MTL", "SF"]),
    html_label("Text Input"),
    dcc_input(value = "MTL", type = "text"),
    html_label("Slider"),
    dcc_slider(
        min = 0,
        max = 9,
        marks = Dict([i => (i == 1 ? "Label $(i)" : "$(i)") for i = 1:6]),
        value = 5,
    )
end

run_server(app, "0.0.0.0", 8000, debug = true)




Help

Dash components are declarative: every configurable aspect of these
components is set during instantiation as a keyword argument.

Call ? in your Julia REPL on any of the components to
learn more about a component and its available arguments.

  help?> dcc_dropdown

  dcc_dropdown(;kwargs...)

  | A Dropdown component.
  | Dropdown is an interactive dropdown element for selecting one or more
  | items.
  | The values and labels of the dropdown items are specified in the `options`
  | property and the selected item(s)
  | are specified with the `value` property.
  | Use a dropdown when you have many options (more than 5) or when you are
  | constrained for space. Otherwise, you can use RadioItems or a Checklist,
  | which have the benefit of showing the users all of the items at once.

  | Keyword arguments:
  | - id (String; optional)
  | - className (String; optional)
  | - disabled (Bool; optional): If true, this dropdown is disabled
  | - multi (Bool; optional): If true, the user can select multiple values
  | - options (optional)
  | - placeholder (String; optional): The grey, default text shown when no option is selected
  | - value (String | Real | Array of String | Reals; optional): The value of the
  | input. If `multi` is false (the default)then value is just a string that
  | corresponds to the values provided in the `options` property. If `multi` is true,
  | then multiple values can be selected at once, and value is an array of items
  | with values corresponding to those in the `options` prop.

Summary

The layout of a Dash app describes what the app looks like.
The layout is a hierarchical tree of components, or a list of components (in Dash 2.17 and later).

The DashHtmlComponents
library
provides classes for all of the HTML
tags and the keyword arguments describe the HTML attributes like style, class, and id.
The DashCoreComponents
library
generates higher-level components like controls and graphs.

For reference, see:

The next part of the Dash Fundamentals covers how to make these apps interactive.
Dash Fundamentals Part 2: Basic Callbacks