Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

763
Visualizações
Render Python seaborn Plot on Django Template

I am trying to render the plot generated by seaborn on Django template.

I have the following code in my views.py:

def booking_hour_plot():
    qs = bookings_today()
    df = read_frame(qs)
    plot = sns.countplot(data=df)
    return plot

def render_stats_page(request):
    #general info
    user_count = total_users_until_now()
    cancel_rate = activity_cancel_rate()

    #activity/booking details
    activity_set = activities_today()
    booking_set = bookings_today()
    plot = booking_hour_plot()

    return render(request, "stats.html", {'name':'Avonmore Statistics', 

    'total_user':int(user_count),
    'cancel_rate': round(float(cancel_rate), 2),
    'booking_count_today': booking_set.count(),
    'activity_count_today': activity_set.count(),

    'activities_today': activity_set,
    'bookings_today': booking_set,
    'booking_hour_plot': plot,
    
    })

And I have a html template called stats.html that containing the plot. The templates directory and views.py are in the same directory:

<li>Real Time Plot: {{booking_hour_plot}}</li>

However, the rendered html page is not really showing the plot. It's more like the plot object: enter image description here

Any Idea how to put the real plot on the html page, rather than this plot object?

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

 Try saving and encoding your plot as png file, then display as <img> element tag by this way:

from io import BytesIO
import base64

def booking_hour_plot():
    qs = bookings_today()
    df = read_frame(qs)
    plot = sns.countplot(data=df)
    plot_file = BytesIO() 
    plot.savefig(plot_file, format='png')
    encoded_file - base64.b64encode(plot_file.getValue())
    return encoded_file

in stats.html:

<li>Real Time Plot: <img src='data:image/png;base64,{{booking_hour_plot}}'/></li>
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda