Quantcast
Channel: Active questions tagged html - Stack Overflow
Viewing all articles
Browse latest Browse all 67411

Embedding seaborn generated plot image in Django Template

$
0
0

I am trying to embed the image generated by the seaborn plot dynamically into the Django HTML template. While when I am trying to save, the correct image gets saved in my directory. But my requirement is to stream this image in HTML page/Template without saving in the directory. But I get a blank white image on the page instead.

def main():

    .........
    .........

    fig = sb.pairplot(kpi)    
    tmpfile = BytesIO()
    fig.savefig(tmpfile, format='png')
    encoded = base64.b64encode(tmpfile.getvalue())    
    return encoded

view.py:

def drop_down(request):
    distribution = main()
    if request.method == 'POST':
        form = ExpenseForm(request.POST)
        print(form.errors)
        if form.is_valid():
            print(form.errors)
            descriptive_stat_var = form.cleaned_data['Variable Names']
            if descriptive_stat_var != '':
                kk = hello(descriptive_stat_var)
                print(kk)
                return render(request, 'core/drop_down.html', {
                    'expense_form': form,
                    'kk': kk.to_html(),
                    'distribution': distribution
                })
            else:
                return render(request, 'core/drop_down.html', {
                    'expense_form': form
                })
    else:
        form = ExpenseForm()
    return render(request, 'core/drop_down.html', {
        'expense_form': form
    })

Html file:

<img src='data:image/png;base64,{{distribution}}'/>

Viewing all articles
Browse latest Browse all 67411

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>