{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Python: Reactive Visualization (Layout)\n", "\n", "Rendering HTML components.\n", "\n", "Adapted from [User Guide](https://plot.ly/dash/)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Installation\n", "\n", "Install `dash` using the following commands\n", "\n", "```bash\n", "pip3 install -U dash\n", "pip3 install -U dash-renderer\n", "pip3 install -U dash-html-components\n", "pip3 install -U dash-core-components\n", "pip3 install -U plotly\n", "```" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": true }, "outputs": [], "source": [ "import dash\n", "from dash.dependencies import Input, Output\n", "import dash_html_components as html\n", "import dash_core_components as dcc\n", "import pandas as pd" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/html": [ "" ], "text/vnd.plotly.v1+html": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "# For Jupyter notebook only\n", "import plotly\n", "plotly.offline.init_notebook_mode()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Hello Dash" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": true }, "outputs": [], "source": [ "app = dash.Dash()\n", "\n", "app.layout = html.Div(children=[\n", " html.H1('Hello Dash',\n", " style={\n", " 'textAlign': 'center',\n", " }),\n", "\n", " html.Div('''\n", " Dash: A web application framework for Python.\n", " ''',\n", " style={\n", " 'textAlign': 'center',\n", " }),\n", "\n", " dcc.Graph(\n", " id='example-graph',\n", " figure={\n", " 'data': [\n", " {'x': [1, 2, 3], 'y': [4, 1, 2], 'type': 'bar', 'name': 'SF'},\n", " {'x': [1, 2, 3], 'y': [2, 4, 5], 'type': 'bar', 'name': 'Montréal'},\n", " ],\n", " 'layout': {\n", " 'title': 'Dash Data Visualization'\n", " }\n", " }\n", " )\n", "])\n" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": true }, "outputs": [], "source": [ "app.css.append_css({\"external_url\": \"https://codepen.io/chriddyp/pen/bWLwgP.css\"})" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ " * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)\n" ] } ], "source": [ "app.server.run()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Enter ESC-I-I to interrupt before proceeding." ] }, { "cell_type": "markdown", "metadata": { "collapsed": true }, "source": [ "## Rendering a `pandas` table" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": true }, "outputs": [], "source": [ "df = pd.read_csv('https://raw.githubusercontent.com/pandas-dev/pandas/master/pandas/tests/data/iris.csv')" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " | SepalLength | \n", "SepalWidth | \n", "PetalLength | \n", "PetalWidth | \n", "Name | \n", "
---|---|---|---|---|---|
0 | \n", "5.1 | \n", "3.5 | \n", "1.4 | \n", "0.2 | \n", "Iris-setosa | \n", "
1 | \n", "4.9 | \n", "3.0 | \n", "1.4 | \n", "0.2 | \n", "Iris-setosa | \n", "
2 | \n", "4.7 | \n", "3.2 | \n", "1.3 | \n", "0.2 | \n", "Iris-setosa | \n", "
3 | \n", "4.6 | \n", "3.1 | \n", "1.5 | \n", "0.2 | \n", "Iris-setosa | \n", "
4 | \n", "5.0 | \n", "3.6 | \n", "1.4 | \n", "0.2 | \n", "Iris-setosa | \n", "