Hi All,

i'm tring to learn web2py, thanks for the wonderful book and for the really 
nice tutorial!
i'm a bit 'stuck' on how to perform a simple task (i guess it is simple) 
because of my ignorance.

.. i'm bring to update dynamic a graph-chart (generated with matplotlib) using 
a form to pass the input the draw-function.


what i have is a controller :

# template_example.py  that 'espone' a function 'variables' , it will display 
an image generated by matplotlib using the 'stem_plot' function (in the same 
.py file)


# import a lib that generate the data from the url gived as input
from ecoopclimate import Climate
#import some matplotlib methodsm numpy ... 
from pylab import stem, setp, grid, savefig, show, gca, subplot, subplots_adjust
import matplotlib as mpl
import matplotlib.pyplot as plt
import datetime as dt
import numpy as np
# plot function
def stem_plot(data, name):
    data[np.isnan(data)]=0
    fig = plt.figure()
    ax = fig.add_subplot(211)
    mindate = int(data[0][0])
    maxdate = int(data[0][-1])
    date2_1 = dt.datetime(mindate, 1, 1)
    date2_2 = dt.datetime(maxdate, 1, 1)
    delta2 = dt.timedelta(days=365)
    dates2 = mpl.dates.drange(date2_1, date2_2, delta2)

    dateFmt = mpl.dates.DateFormatter('%Y')
    ax.xaxis.set_major_formatter(dateFmt)
    fig.autofmt_xdate(bottom=0.1) 
    x_p = dates2[np.where(data[1]>=0)[0]]
    y_p = data[1][np.where(data[1]>=0)[0]]
    x_n = dates2[np.where(data[1]<0)[0]]
    y_n = data[1][np.where(data[1]<0)[0]]

    markerline, stemlines, baseline = stem(x_p, y_p, 'r-')
    setp(markerline, 'markerfacecolor', 'b')
    setp(baseline, 'color', 'r', 'linewidth', 2)
    setp(stemlines, 'linewidth', 1)
    markerline, stemlines, baseline = stem(x_n, y_n, 'b-')
    setp(markerline, 'markerfacecolor', 'b')
    setp(baseline, 'color', 'r', 'linewidth', 2)
    setp(stemlines, 'linewidth', 1)
    grid()
    setp(gca(), 'xlabel', 'Year', 'ylabel', name)
    fig = '/Users/epifanio/web2py/applications/welcome/static/'+name+'.png'
    dateFmt = mpl.dates.DateFormatter('%Y')
    savefig(fig)
    return fig
    #show()

# function connected to the view : variables.html
def variables():
    nao_url = 
'https://climatedataguide.ucar.edu/sites/default/files/cas_data_files/asphilli/nao_station_djfm.txt'
    clm = Climate()
    NAO = clm.nao(nao_url, 'nao.txt')
    image = stem_plot(NAO['nao'], 'nao')
    return dict(d=URL('static', 'nao.png'))


the view template_example/variables.html is : 

{{extend 'layout.html'}}
<h1>Image generated with matplotlib</h1>

<img src="{{=d}}" alt="Red dot" />


As you can see from the code in stem_plot it takes as input :  'data' and 'name'
i'm hardcoding the var inside the function 'variables' 

i need to modify that code adding a 'form' in the view that will take 2 string 
as input

nao_url = 
'https://climatedataguide.ucar.edu/sites/default/files/cas_data_files/asphilli/nao_station_djfm.txt'
image_name = 'imagename'

and pass this  input to the main function that generate the graph.
Have you any hints on how to do that ?
i'd like to have the form in the same page where the image is displayed
so that i can update the image every time i'll resubmit the form.
i'm a beginner, an example that does something like that will be really helpful 
:)
Many many Thanks for your help! 
Massimo.




 

Reply via email to