2016-04-25 11 views
10

Django html şablonuna çizim grafiği yerleştirmeye çalışıyorum. Bu grafik, "çevrimiçi modda" üretildiğinde iyi çalışır (yani, html snippet'i arsa sunucuda saklanır) ancak 'çevrimdışı modda' (yani html yerel olarak saklandığında) değil. Son durumda, grafik görünmez. Html'yi yerel sunucumda depolayabilmek ve arsaları oradan gömmek istiyorum. İşte Django şablonuna Çizim grafiği ekleme

işleri biraz: Http bir dize Django isteği işlemek olarak pie_url geçirilir

import plotly.plotly as py 
import plotly.graph_objs as go 
labels = [1,2,3,4] 
values = [10,20,30,40] 
ndata = 100 
fig = { 
    'data': [{'labels': labels, 
      'values': values, 
      'type': 'pie', 
      'textposition':"none", 
      'textinfo':"percent", 
      'textfont':{'size':'12'}, 
      'showlegend':'false'}], 
    'layout': {'title': 'Total:'+str(ndata), 
      'showlegend':'false', 
      'height':'200', 
      'width':'200', 
      'autosize':'false', 
      'margin':{'t':'50','l':'75','r':'0','b':'10'}, 
      'separators':'.,'} 
} 
plotly_url = py.plot(fig, filename='myfile', auto_open=False) 
pie_url = '<iframe width="200" height="200" frameborder="0" seamless="seamless" scrolling="no" src='+plotly_url+'.embed?width=200&height=200&link=false&showlegend=false></iframe>' 

Not söyledi. Şablon dizeyi html olarak | güvenli etiketi, yani {{pie_url | safe}}.

from plotly.offline import download_plotlyjs, plot 
import plotly.graph_objs as go 
labels = [1,2,3,4] 
values = [10,20,30,40] 
ndata = 100 
fig = { 
    'data': [{'labels': labels, 
      'values': values, 
      'type': 'pie', 
      'textposition':"none", 
      'textinfo':"percent", 
      'textfont':{'size':'12'}, 
      'showlegend':'false'}], 
    'layout': {'title': 'Total:'+str(ndata), 
      'showlegend':'false', 
      'height':'200', 
      'width':'200', 
      'autosize':'false', 
      'margin':{'t':'50','l':'75','r':'0','b':'10'}, 
      'separators':'.,'} 
} 
plotly_url = plot(fig, filename='file:///home/website/pie.html', auto_open=False) 
pie_url = '''<iframe width="200" height="200" frameborder="0" seamless="seamless" scrolling="no" src=\"'''+plotly_url+'''.embed?width=200&height=200&link=false&showlegend=false\"></iframe>''' 

Herhangi bir tavsiye mutluluk duyacağız: Burada

çalışmıyor biraz.

+1

musunuz? – frankenapps

+0

Merhaba, Evet, html dosyası üretiliyor. Ancak, Django bunu oluşturduğunda görünmez (bu, özgün gönderideki pie_url satırıdır.) – Hooloovoo

cevap

20

Bir dosyaya html yazmak yerine grafiğin html bölümünü bir dize olarak grafiğe dönüştürebilirsiniz. Örneğin, bir sınıf temelli TemplateView kullanarak:

import plotly.offline as opy 
import plotly.graph_objs as go 

class Graph(TemplateView): 
    template_name = 'graph.html' 

    def get_context_data(self, **kwargs): 
     context = super(Graph, self).get_context_data(**kwargs) 

     x = [-2,0,4,6,7] 
     y = [q**2-q+3 for q in x] 
     trace1 = go.Scatter(x=x, y=y, marker={'color': 'red', 'symbol': 104, 'size': "10"}, 
          mode="lines", name='1st Trace') 

     data=go.Data([trace1]) 
     layout=go.Layout(title="Meine Daten", xaxis={'title':'x1'}, yaxis={'title':'x2'}) 
     figure=go.Figure(data=data,layout=layout) 
     div = opy.plot(figure, auto_open=False, output_type='div') 

     context['graph'] = div 

     return context 

ve şablonu: Bir .html dosyası çıktı onu mümkün

{% if graph %} 
<div style="width:600;height:500"> 
{{ graph|safe }} 
</div> 
{% endif %} 
+1

Bu bağlamı görüntülemek için bunu büyük olasılıkla görüntüleyebilirsiniz. 'g = Grafik()' 'context = g.get_context_data() ' ' döndürme işlemi (istek, 'uygulama/stats.html', bağlam) ' DÜZENLEME: URL'lerde doğrudan TemplateViews kullanın https://docs.djangoproject.com/en/1.10/ref/class-based- Gösterim/baz/# templateview – Naman

İlgili konular