Creating dynamic charts using Matplotlib in Django

So, I have 2 views: the first one generates html on demand, the second one creates a chart to display for the first view.

HTML view

def activation_signupcount(request):

    if 'datestart' not in request.GET:

        return render_to_response('activation/activation_signupcount.html', {'datestart':''})

    else:

        datestart = request.GET['datestart']
        dateend = request.GET['dateend']

        return render_to_response('activation/activation_signupcount.html', {'datestart':datestart, 'dateend':dateend})#

CHART Review

def activation_signupcount_graph(request):

    datestart = request.GET['datestart'] #this doesnt work
    dateend = request.GET['dateend'] #this doesnt work

    print datestart,dateend

    # open sql connection
    cursor = connection.cursor()
    # execute query
    cursor.execute("SELECT COUNT(1), JoinDate FROM users WHERE JoinDate BETWEEN '"+ datestart +"' AND '"+ dateend +"' GROUP BY JoinDate;")
    # close connection

    data = cursor.fetchall()

    cursor.close()
    connection.close() 

    fig = Figure()
        ax = fig.add_subplot(111)

    x = []
    y = []

    x = [k[1] for k in data]
    y = [k[0] for k in data]

    ax.plot_date(x, y, '-')
    ax.xaxis.set_major_formatter(DateFormatter('%Y-%m-%d'))
    fig.autofmt_xdate()
    canvas = FigureCanvas(fig)
    response = HttpResponse(content_type='image/png')
    canvas.print_png(response)

    return response

So, on the page activation/activation_signupcount.html, I have 2 date fields, a start and an end, which sends a GET request. So my question is: how can I parse these two date variables of my function activation_signupcount_graphto get start and end dates to create a chart?

Hope this was clear!

+1
source share
2 answers

You can access your graphic representation in your template using url-templatetag with the appropriate parameters.

, :

<img src="{% url yourapp.chart_view start_date end_date %}" />

, get-parameters:

<img src="{% url yourapp.chart_view %}?datestart={{ datestart }}" />
+3

( pySVG SVG, btw), matplotlib virtualenvs.

matplotlib ( ubuntu) virtualenvs pip... anthem

0

All Articles