0

Unable to dynamically update data visualization charts via user input.

make_visual takes info, creates matplotlib visuals, then saves them in the static location.

import matplotlib
matplotlib.use('agg')
import matplotlib.pyplot as plt

def make_visual(nums):
    a, b, c, d, e, f, g, h = nums
    turn_labels = "A", "B"
    turn_size = [a,b]
    goes_labels = "1", "2"
    goes_size = [c,d]
    method_labels = "Moo", "Bark", "Meow"
    method_size = [g, f, e]
    fig1, ax1 = plt.subplots()
    ax1.pie(turn_size, labels=turn_labels, autopct='%1.1f%%', startangle=90)
    ax1.axis('equal')
    plt.savefig("static/graph.png")
    fig2, ax2 = plt.subplots()
    ax2.pie(goes_size, labels=goes_labels, autopct='%1.1f%%', startangle=90)
    ax2.axis('equal')
    plt.savefig("static/goes.png")
    fig3, ax3 = plt.subplots()
    ax3.pie(method_size, labels=method_labels, autopct='%1.1f%%', startangle=90)
    ax3.axis('equal')
    plt.savefig("static/method.png")

make_visual([0.50, 0.50, 0.4, 0.6, 0.43, 0.3, 0.27, 14.511904761904763])

^ When this runs on it's own in the command line, it works. Does not work in the Flask function. If I remove make_visual, the info goes through to the results.html page as intended.

I am unsure as to why I am unable to dynamically create, save, then render the page with the visuals current from the static folder.

@app.route('/results', methods=['POST'])
def results():
    stats = request.form['data']
    info = get_stats(stats)
    unstr = stats.split(',')
    title = make_string(unstr[0], unstr[1])
    make_visual(info)
    return render_template('results.html', stats=title, data=info)

Below is results.html.

{% block content %}
<div class='container-fluid text-center'>
        <h1 class='p-2 m-2'>Results</h1>
        <h3 class='text-success'>{{ stats }}</h3>
        <h4>{{ data }}</h4>
    <div class="row mt-2">
        <div class="col-lg-4 p-1">
            <img class='img-fluid' src="/static/graph.png">
        </div>
        <div class="col-lg-4 p-1">
            <img class='img-fluid' src="/static/goes.png">
        </div>
        <div class="col-lg-4 p-1">
            <img class='img-fluid' src="/static/method.png">
        </div>
    </div>
{% endblock %}
0

1 Answer 1

1

For best results, show code that demonstrates the problem, with extraneous details removed and in a full enough form that it can be run. Often the exercise of doing that is sufficient to unearth the cause.

However, getting matplotlib working with Flask involves a trick. I'm guessing that after

import matplotlib

there's no

matplotlib.use('agg')

without with matplotlib will attempt to use tkinter to render, which leads to sadness when trying to work with Flask.

Sign up to request clarification or add additional context in comments.

1 Comment

Good guess and idea. I added the make_visual function to the question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.