-
Notifications
You must be signed in to change notification settings - Fork 127
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #39 from delsim/html_formation
Html formation to enable dash app without iframe
- Loading branch information
Showing
19 changed files
with
536 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
'''Dash demonstration application | ||
TODO attribution here | ||
''' | ||
|
||
# The linter doesn't like the members of the html and dcc imports (as they are dynamic?) | ||
#pylint: disable=no-member | ||
|
||
import dash | ||
import dash_core_components as dcc | ||
import dash_html_components as html | ||
import plotly.graph_objs as go | ||
#import dpd_components as dpd | ||
import numpy as np | ||
from django_plotly_dash import DjangoDash | ||
|
||
#from .urls import app_name | ||
app_name = "DPD demo application" | ||
|
||
dashboard_name1 = 'dash_example_1' | ||
dash_example1 = DjangoDash(name=dashboard_name1, | ||
serve_locally=True, | ||
app_name=app_name | ||
) | ||
|
||
# Below is a random Dash app. | ||
# I encountered no major problems in using Dash this way. I did encounter problems but it was because | ||
# I was using e.g. Bootstrap inconsistenyly across the dash layout. Staying consistent worked fine for me. | ||
dash_example1.layout = html.Div(id='main', | ||
children=[ | ||
html.Div([dcc.Dropdown(id='my-dropdown1', | ||
options=[{'label': 'New York City', 'value': 'NYC'}, | ||
{'label': 'Montreal', 'value': 'MTL'}, | ||
{'label': 'San Francisco', 'value': 'SF'} | ||
], | ||
value='NYC', | ||
className='col-md-12', | ||
), | ||
html.Div(id='test-output-div') | ||
]), | ||
|
||
dcc.Dropdown( | ||
id='my-dropdown2', | ||
options=[ | ||
{'label': 'Oranges', 'value': 'Oranges'}, | ||
{'label': 'Plums', 'value': 'Plums'}, | ||
{'label': 'Peaches', 'value': 'Peaches'} | ||
], | ||
value='Oranges', | ||
className='col-md-12', | ||
), | ||
|
||
html.Div(id='test-output-div2') | ||
|
||
]) # end of 'main' | ||
|
||
@dash_example1.expanded_callback( | ||
dash.dependencies.Output('test-output-div', 'children'), | ||
[dash.dependencies.Input('my-dropdown1', 'value')]) | ||
def callback_test(*args, **kwargs): #pylint: disable=unused-argument | ||
'Callback to generate test data on each change of the dropdown' | ||
|
||
# Creating a random Graph from a Plotly example: | ||
N = 500 | ||
random_x = np.linspace(0, 1, N) | ||
random_y = np.random.randn(N) | ||
|
||
# Create a trace | ||
trace = go.Scatter(x=random_x, | ||
y=random_y) | ||
|
||
data = [trace] | ||
|
||
layout = dict(title='', | ||
yaxis=dict(zeroline=False, title='Total Expense (£)',), | ||
xaxis=dict(zeroline=False, title='Date', tickangle=0), | ||
margin=dict(t=20, b=50, l=50, r=40), | ||
height=350, | ||
) | ||
|
||
|
||
fig = dict(data=data, layout=layout) | ||
line_graph = dcc.Graph(id='line-area-graph2', figure=fig, style={'display':'inline-block', 'width':'100%', | ||
'height':'100%;'}) | ||
children = [line_graph] | ||
|
||
return children | ||
|
||
|
||
@dash_example1.expanded_callback( | ||
dash.dependencies.Output('test-output-div2', 'children'), | ||
[dash.dependencies.Input('my-dropdown2', 'value')]) | ||
def callback_test2(*args, **kwargs): | ||
'Callback to exercise session functionality' | ||
|
||
print(args) | ||
print(kwargs) | ||
|
||
children = [html.Div(["You have selected %s." %(args[0])]), | ||
html.Div(["The session context message is '%s'" %(kwargs['session_state']['django_to_dash_context'])])] | ||
|
||
return children |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
{%extends "base.html"%} | ||
{%load plotly_dash%} | ||
|
||
{%block title%}Demo One - Simple Embedding{%endblock%} | ||
|
||
{%block content%} | ||
<h1>Direct App Embedding</h1> | ||
<p> | ||
This is a simple example of use of a dash application within a Django template. Use of | ||
the plotly_direct template tag with the name of a dash application causes | ||
the Dash application to be directly embedded within the page. | ||
</p> | ||
<p> | ||
The plotly_class tag is also used to wrap the application in css class names based on the | ||
application (django-plotly-dash), the | ||
type of the embedding (here labelled "div-direct"), and the slugified version of the app name (simpleexample). | ||
</p> | ||
<div class="card bg-light border-dark"> | ||
<div class="card-body"> | ||
<p><span>{</span>% load plotly_dash %}</p> | ||
<p><div class="<span>{</span>% plotly_class name="SimpleExample" template_type="div-direct"%}"> | ||
<p class="ml-3"><span>{</span>% plotly_direct name="SimpleExample" %}</p> | ||
<p><\div> | ||
</div> | ||
</div> | ||
<p></p> | ||
<div class="card border-dark"> | ||
<div class="card-body"> | ||
<div class="{%plotly_class name="SimpleExample" template_type="div-direct"%}"> | ||
{%plotly_direct name="SimpleExample"%} | ||
</div> | ||
</div> | ||
</div> | ||
<p></p> | ||
{%endblock%} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
{%extends "base.html"%} | ||
{%load plotly_dash%} | ||
|
||
{%block title%}Demo Six - Simple Injection{%endblock%} | ||
|
||
{%block content%} | ||
|
||
<h1>Simple embedding</h1> | ||
|
||
<p> | ||
Direct insertion of html into a web page. | ||
</p> | ||
|
||
<p> | ||
This demo is based on a contribution by, and | ||
with thanks to, <a href="https://github.com/eddy-ojb">@eddy-ojb</a> | ||
</p> | ||
|
||
<div class="card bg-light border-dark"> | ||
<div class="card-body"> | ||
<p><span>{</span>% load plotly_dash %}</p> | ||
<p><div class="<span>{</span>% plotly_class name="dash_example_1" template_type="div-direct"%}"> | ||
<p class="ml-3"><span>{</span>% plotly_direct name="dash_example_1" %}</p> | ||
<p><\div> | ||
</div> | ||
</div> | ||
<p></p> | ||
<div class="card border-dark"> | ||
<div class="card-body"> | ||
<div class="{%plotly_class name="dash_example_1" template_type="div-direct"%}"> | ||
{%plotly_direct name="dash_example_1"%} | ||
</div> | ||
</div> | ||
</div> | ||
<p></p> | ||
{%endblock%} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
''' | ||
Example view generating non-trivial content | ||
''' | ||
|
||
from django.shortcuts import render | ||
|
||
#pylint: disable=unused-argument | ||
|
||
def dash_example_1_view(request, template_name="demo_six.html", **kwargs): | ||
'Example view that inserts content into the dash context passed to the dash application' | ||
|
||
context = {} | ||
|
||
# create some context to send over to Dash: | ||
dash_context = request.session.get("django_plotly_dash", dict()) | ||
dash_context['django_to_dash_context'] = "I am Dash receiving context from Django" | ||
request.session['django_plotly_dash'] = dash_context | ||
|
||
return render(request, template_name=template_name, context=context) |
Oops, something went wrong.