Skip to content

Commit 3d38a72

Browse files
author
Mark Gibbs
committed
Adding demo of injected html from eddy-ojb
1 parent 4798532 commit 3d38a72

File tree

6 files changed

+184
-18
lines changed

6 files changed

+184
-18
lines changed

demo/demo/dash_apps.py

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
'''Dash demonstration application
2+
3+
TODO attribution here
4+
'''
5+
6+
import dash
7+
import dash_core_components as dcc
8+
import dash_html_components as html
9+
import plotly.graph_objs as go
10+
#import dpd_components as dpd
11+
import numpy as np
12+
from django_plotly_dash import DjangoDash
13+
14+
#from .urls import app_name
15+
app_name ="DPD demo application"
16+
17+
dashboard_name1 = 'dash_example_1'
18+
dash_example1 = DjangoDash(name=dashboard_name1,
19+
serve_locally=True,
20+
app_name=app_name
21+
)
22+
23+
# Below is a random Dash app.
24+
# I encountered no major problems in using Dash this way. I did encounter problems but it was because
25+
# I was using e.g. Bootstrap inconsistenyly across the dash layout. Staying consistent worked fine for me.
26+
dash_example1.layout = html.Div(id='main',
27+
children=[
28+
29+
html.Div([
30+
dcc.Dropdown(
31+
id='my-dropdown1',
32+
options=[
33+
{'label': 'New York City', 'value': 'NYC'},
34+
{'label': 'Montreal', 'value': 'MTL'},
35+
{'label': 'San Francisco', 'value': 'SF'}
36+
],
37+
value='NYC',
38+
className='col-md-12',
39+
40+
),
41+
html.Div(id='test-output-div')
42+
]),
43+
44+
dcc.Dropdown(
45+
id='my-dropdown2',
46+
options=[
47+
{'label': 'Oranges', 'value': 'Oranges'},
48+
{'label': 'Plums', 'value': 'Plums'},
49+
{'label': 'Peaches', 'value': 'Peaches'}
50+
],
51+
value='Oranges',
52+
className='col-md-12',
53+
),
54+
55+
html.Div(id='test-output-div2')
56+
57+
]) # end of 'main'
58+
59+
@dash_example1.expanded_callback(
60+
dash.dependencies.Output('test-output-div', 'children'),
61+
[dash.dependencies.Input('my-dropdown1', 'value')])
62+
def callback_test(*args, **kwargs):
63+
64+
# Creating a random Graph from a Plotly example:
65+
N = 500
66+
random_x = np.linspace(0, 1, N)
67+
random_y = np.random.randn(N)
68+
69+
# Create a trace
70+
trace = go.Scatter(
71+
x = random_x,
72+
y = random_y
73+
)
74+
75+
data = [trace]
76+
77+
layout = dict(title='',
78+
yaxis = dict(zeroline = False, title='Total Expense (£)',),
79+
xaxis = dict(zeroline = False, title='Date', tickangle=0),
80+
margin=dict(t=20, b=50, l=50, r=40),
81+
height=350,
82+
)
83+
84+
85+
fig = dict(data=data, layout=layout)
86+
line_graph = dcc.Graph(id='line-area-graph2', figure=fig, style={'display':'inline-block', 'width':'100%',
87+
'height': '100%;'} )
88+
children = [line_graph]
89+
90+
return children
91+
92+
93+
@dash_example1.expanded_callback(
94+
dash.dependencies.Output('test-output-div2', 'children'),
95+
[dash.dependencies.Input('my-dropdown2', 'value')])
96+
def callback_test2(*args, **kwargs):
97+
98+
print(args)
99+
print(kwargs)
100+
101+
children = [html.Div(["You have selected %s." %(args[0])]),
102+
html.Div(["The session context message is '%s'" %(kwargs['session_state']['django_to_dash_context'])])]
103+
104+
return children

demo/demo/templates/base.html

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
<!DOCTYPE HTML>
22
<html>
33
<head>
4-
{%load plotly_dash%}
5-
{%load staticfiles%}
6-
{%load bootstrap4%}
7-
{%bootstrap_css%}
8-
{%bootstrap_javascript jquery="full"%}
9-
{%block extra_header%}{%endblock%}
10-
{%block app_header_css%}
4+
{% load plotly_dash%}
5+
{% load staticfiles%}
6+
{% load bootstrap4%}
7+
{% bootstrap_css%}
8+
{% bootstrap_javascript jquery="full"%}
9+
{% block extra_header%}{% endblock%}
10+
{% block app_header_css%}
1111
<link rel="stylesheet" type="text/css" href="{%static "demo/demo.css"%}"></link>
12-
{%endblock%}
13-
{%plotly_header%}
14-
<title>Django Plotly Dash Examples - {%block title%}{%endblock%}</title>
12+
{% endblock%}
13+
{% plotly_header%}
14+
<title>Django Plotly Dash Examples - {% block title%}{% endblock%}</title>
1515
</head>
1616
<body>
1717
<header>
@@ -20,28 +20,33 @@
2020
<a class="navbar-brand" href="#">
2121
<img src="{%static "demo/logo.svg"%}" alt="Logo"/>
2222
</a>
23-
{%block demo_items%}
23+
{% block demo_items %}
2424
<a class="nav-item nav-link btn btn-lg" href="{%url "home"%}">Contents</a>
25+
{% block demo_menu_items %}
26+
{% if 0 %}
2527
<a class="nav-item nav-link btn btn-lg" href="{%url "demo-one"%}">One - Simple Use</a>
2628
<a class="nav-item nav-link btn btn-lg" href="{%url "demo-two"%}">Two - Initial State</a>
2729
<a class="nav-item nav-link btn btn-lg" href="{%url "demo-three"%}">Three - Enhanced Callbacks</a>
2830
<a class="nav-item nav-link btn btn-lg" href="{%url "demo-four"%}">Four - Live Updating</a>
29-
<a class="nav-item nav-link btn btn-lg" href="{%url "demo-five"%}">Five - Direct Embedding</a>
31+
<a class="nav-item nav-link btn btn-lg" href="{%url "demo-five"%}">Five - Direct Injection</a>
32+
<a class="nav-item nav-link btn btn-lg" href="{%url "demo-six"%}">Six - Simple Injection</a>
33+
{% endif %}
34+
{% endblock %}
3035
<a class="nav-item nav-link btn btn-lg"
3136
target="_blank"
3237
href="https://django-plotly-dash.readthedocs.io/en/latest/">Online Documentation</a>
33-
{%endblock%}
38+
{% endblock %}
3439
</div>
3540
</nav>
3641
</header>
3742
<main>
3843
<div class="container">
39-
{%block content%}{%endblock%}
44+
{% block content%}{% endblock%}
4045
</div>
4146
</main>
42-
{%block footer%}
43-
{%endblock%}
47+
{% block footer%}
48+
{% endblock%}
4449
</body>
45-
{%block post_body%}{%endblock%}
46-
{%plotly_footer%}
50+
{% block post_body%}{% endblock%}
51+
{% plotly_footer%}
4752
</html>

demo/demo/templates/demo_six.html

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
{%extends "base.html"%}
2+
{%load plotly_dash%}
3+
4+
{%block title%}Demo Six - Simple Injection{%endblock%}
5+
6+
{%block content%}
7+
8+
<h1>Simple embedding</h1>
9+
10+
<p>
11+
Direct insertion of html into a web page.
12+
</p>
13+
14+
<p>
15+
This demo is based on a contribution by, and
16+
with thanks to, <a href="https://github.com/eddy-ojb">@eddy-ojb</a>
17+
</p>
18+
19+
<div class="card bg-light border-dark">
20+
<div class="card-body">
21+
<p><span>{</span>% load plotly_dash %}</p>
22+
<p>&lt;div class="<span>{</span>% plotly_class name="dash_example_1" template_type="div-direct"%}">
23+
<p class="ml-3"><span>{</span>% plotly_direct name="dash_example_1" %}</p>
24+
<p>&lt;\div>
25+
</div>
26+
</div>
27+
<p></p>
28+
<div class="card border-dark">
29+
<div class="card-body">
30+
<div class="{%plotly_class name="dash_example_1" template_type="div-direct"%}">
31+
{%plotly_direct name="dash_example_1"%}
32+
</div>
33+
</div>
34+
</div>
35+
<p></p>
36+
{%endblock%}

demo/demo/templates/index.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,6 @@ <h1>Demonstration Application</h1>
1212
<li><a class="btn btn-primary btnspace" href="{%url "demo-three"%}">Demo Three</a> - adding Django features with enhanced callbacks</li>
1313
<li><a class="btn btn-primary btnspace" href="{%url "demo-four"%}">Demo Four</a> - live updating of apps by pushing from the Django server</li>
1414
<li><a class="btn btn-primary btnspace" href="{%url "demo-five"%}">Demo Five</a> - injection of a Dash application without embedding in an html iframe</li>
15+
<li><a class="btn btn-primary btnspace" href="{%url "demo-six"%}">Demo Six</a> - simple html injection example</li>
1516
</ul>
1617
{%endblock%}

demo/demo/urls.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,20 @@
2727

2828
# Load demo plotly apps - this triggers their registration
2929
import demo.plotly_apps # pylint: disable=unused-import
30+
import demo.dash_apps # pylint: disable=unused-import
3031

3132
from django_plotly_dash.views import add_to_session
3233

34+
from .views import dash_example_1_view
35+
3336
urlpatterns = [
3437
url('^$', TemplateView.as_view(template_name='index.html'), name="home"),
3538
url('^demo-one$', TemplateView.as_view(template_name='demo_one.html'), name="demo-one"),
3639
url('^demo-two$', TemplateView.as_view(template_name='demo_two.html'), name="demo-two"),
3740
url('^demo-three$', TemplateView.as_view(template_name='demo_three.html'), name="demo-three"),
3841
url('^demo-four$', TemplateView.as_view(template_name='demo_four.html'), name="demo-four"),
3942
url('^demo-five$', TemplateView.as_view(template_name='demo_five.html'), name="demo-five"),
43+
url('^demo-six', dash_example_1_view, name="demo-six"),
4044
url('^admin/', admin.site.urls),
4145
url('^django_plotly_dash/', include('django_plotly_dash.urls')),
4246

demo/demo/views.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
'''
2+
Example view generating non-trivial content
3+
'''
4+
5+
from django.shortcuts import render
6+
7+
def dash_example_1_view(request, template_name="demo_six.html",**kwargs):
8+
9+
context = {}
10+
11+
# create some context to send over to Dash:
12+
dash_context = request.session.get("django_plotly_dash", dict())
13+
dash_context['django_to_dash_context'] = "I am Dash receiving context from Django"
14+
request.session['django_plotly_dash'] = dash_context
15+
16+
return render(request, template_name=template_name, context=context)

0 commit comments

Comments
 (0)