Matplotlib.pyplot with Django

Asked

Viewed 259 times

0

I’m trying to insert a graph through the matlib.pyplot library, but I can only upload it externally by inserting it into Jango. I want to insert it as a loaded image, that when receiving data from the database, it generates an image whenever it receives new data.

The current code looks like this: Views.py

def dashboard(request):
dados = Usuario.objects.all().order_by('-created_at')
x = [6, 7, 4]  # Base
y = [2, 6, 2]  # Altura

titulo = "Hello World"
eixox = "Eixo X"
eixoy = "Eixo Y"

grafico = plt.title(titulo)
grafico = plt.xlabel(eixox)
grafico = plt.ylabel(eixoy)
grafico = plt.plot(x, y)
grafico = plt.savefig('save.png', dpi=72)
return render(request, 'Usuario/dash.html', {'dados': dados, 'grafico': grafico})

I want to make a Dashboard with User data and that on top appear metrics on a certain attribute of the model, but here I am only tested with simple data and I had tested alone.

In Dash.html it looks like this:

{% extends 'base.html' %}
{% block title %} DashBoard {% endblock %}


 {% block content %}

 <div>
 <img src="save.png">
 </div>
 <h1 class="mb-5">DashBoard</h1>
<table class="table">
    <thead class="thead-dark">
        <tr>
        <th scope="col">Tipo Usuário</th>
        <th scope="col">Status Usuário</th>
        <th scope="col">Nome do Usuário</th>
        <th scope="col">Login</th>
        <th scope="col">Última Atualização</th>
        </tr>
    </thead>
    <tbody>
{% if messages %}
    <ul class="messages">
        {% for message in messages %}
            <li {% if message.tags %}class="alert alert-success" {% endif %}>{{ message }}</li>
        {% endfor %}
    </ul>
{%  endif %}
    {% for dados in dados %}
        <tr>
        <th scope="row">{{dados.Tipo_Usuario}} </th>
        <td>{{dados.Tipo_Status_Usuario}}</td>
        <td>{{dados.Nome_Usuario}}</td>
        <td>{{dados.Login_Usuario}}</td>
        <td>{{dados.updated_at}}</td>
        </tr>
    {% endfor %}
    </tbody>
</table>

{% endblock %}

Image saved but not shown in html

I’ve been at it for more than three days, I’ve looked at foreign forums and here too, but I can’t solve it with their solutions.

  • Which folder the image is being saved to?

  • I was saving in the same root folder, to test if it appeared or if at least it was generated, but it did not work.

  • I kept thinking for days, and basically, it was being generated in one folder and I was calling for another. Since Django uses the media folder to store the media and I opened a specific folder for it. I basically inserted in views graphic = plt.savefig('media/graphic/Graficos.png', dpi=1200) and inside the template I put <img src="media/graphic/Graficos.png" style="width:500px;height:500px;"> It must be the dawn that brought me this ease in encoding kkk Thanks drec4s

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.