i am creating div and then rendering to the template file in django
below is code for the view.py
def get_chart_data(request):
import numpy as np
import plotly.graph_objs as go
from plotly.offline import plot
x_data = []
y_data=[]
y_data2=[]
for i in range(1,100):
x_data.append(i)
y_data.append(random.randint(1,4)*random.randint(9,400))
y_data2.append(random.randint(1,4)*random.randint(9,40))
trace1 = go.Bar(
x=x_data,
y=y_data,
# mode='markers',
name='today'
)
trace2 = go.Bar(
x=x_data,
y=y_data,
name='yesterday'
# mode='markers',
)
data = [trace1,trace2]
layout = go.Layout(
autosize=True,
#width=900,
#height=500,
barmode = 'relative', # group/relative/stack
xaxis=dict(
autorange=True
),
yaxis=dict(
autorange=True
)
)
fig = go.Figure(data=data, layout=layout)
plot_div = plot(fig, output_type='div', include_plotlyjs=False)
langs = ['C', 'C++', 'Java', 'Python', 'PHP']
students = [23,17,35,29,12]
trace = go.Pie(labels = langs, values = students)
data = [trace]
fig1=go.Figure(data=data)
plot_div1 = plot(fig1, output_type='div', include_plotlyjs=False)
return render(request,'refresh_plotly.html',{'plot':plot_div,'plot1':plot_div1})```
and below is the template file
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src="https://cdn.plot.ly/plotly-latest.min.js"></script>
<script src="/static/assets/js/axios.min.js"></script>
<script src="/static/assets/plugins/jquery/jquery.min.js"></script>
<!-- jQuery UI 1.11.4 -->
<script src="/static/assets/plugins/jquery-ui/jquery-ui.min.js"></script>
<!-- Resolve conflict in jQuery UI tooltip with Bootstrap tooltip -->
<div id="jam">
{{plot|safe}}
{{plot1|safe}}
</div>
<script>
var sitename = "JAM"
var plotly = document.getElementById("jam");
function loadPlotly() {
axios.get('/refresh_plotly', {
params: {
site: sitename
}
}).then(function (resp) {
console.log(resp.data);
plotly.innerHTML = resp.data
}).catch(function (err) {
console.log(err)
})
}
setInterval(function () {
loadPlotly();
}, 3000);
</script>
```
if i want to reload the div again by calling some other URL ( which offcourse gives same giv ) it is not refreshing.
when i put resp.data in html file and open graph is there but it is noty getting update live