Problem passing plotly graph from function in python

I am trying to define in python a function to create a plot and then pass the plot to another function. The plot works if I directly render it in a Django template, but If I return, to the function that called the graphing function, and pass the returned plot_div via context to the Django template, no graph gets drawn when I try to pass it to the template in context.

def newplotly(request):
(Much code to draw graph deleted for space)
    plot_div = pyo.plot(fig, output_type='div')

    return plot_div