0

I am writing a code that displays a plotly graph in a django template. The source of data should come from a pandas dataframe.

I have tried the one shared from this page: Embedding a Plotly chart in a Django template

and modified it as follows:

def plotly_scatter(df, _x):
x = df.iloc[:, 1:2]
y = df.iloc[:, 3:4]

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')

return div 

Have this function called from a view with the following line

try:
        df = pd.read_csv(project.base_file)
        context['graph']   = plotly_scatter(df, 1)
    except: pass 

and tried his template:

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

The graph plot is displayed but no line is displayed. Here is my output: enter image description here

Here is the original output:

enter image description here

dJudge
  • 186
  • 2
  • 17
  • What result did you get? – Endre Both Mar 15 '19 at 13:46
  • @EndreBoth, the first image is my result. – dJudge Mar 15 '19 at 13:54
  • So it does output something, but not exactly what you need. First I'd try to find out where the error happens: In the Plotly code, in your processing or in the template. To do so, I'd probably save the output to a file at various points in the code and compare it to the expected output. Debugging is a chore... – Endre Both Mar 15 '19 at 14:56

0 Answers0