1
I have two functions, I need the list of the first to be read by the second, the two are in a view of Django, that each generates a different template.
class Tranfer:
shared = [] #Essa proprieda sera compartilhada para cada instancia desta
classe
def __init__(self, *args):
self.shared.extend(args)
def reset(self):
self.shared = []
def gerar_graficos(request):
descricao = None
descricao2 = None
if request.method=='POST':
descricao = request.POST['descricao']
descricao2 = request.POST['descricao2']
conn = cx_Oracle.connect('banco de dados')
cur = conn.cursor()
cur.execute(consulta.format(descricao, descricao2))
teste = []
teste2 = []
teste3 = []
teste4 = []
global dados
global data
d = 0
for line in cur:
teste.extend(line)
for indice, c in enumerate(teste):
#if c + 0 == c:
teste3.extend([c - 10000])
#else:
d = 0
dados = teste3
cur.execute(consulta.format(descricao, descricao2))
for coluna in cur:
teste2.extend(coluna)
for indice2, c in enumerate(teste2):
if indice2 >= d:
teste4.extend([c])
data = teste4
transfer = Tranfer()
transfer.reset()
transfer.extend(teste3,teste4)
y_axis = teste3
x_axis = teste4
width_n = 0.001
bar_color = 'yellow'
cur.close()
conn.close()
context = { 'descricao': descricao, 'descricao2': descricao2 }
return render(request, 'core/graficos_list.html', context)
Pass the test lists 3 and 4 to the function below.
def index(request):
trans = Tranfer()
my_list = trans.shared[0]
my_list2 = trans.shared[1]
print (my_list)
fig = Figure()
ax = fig.add_subplot(1,1,1)
ax.plot(my_list, my_list2)
ax.grid()
buf = io.BytesIO()
canvas = FigureCanvas(fig)
canvas.print_png(buf)
response=HttpResponse(buf.getvalue(), content_type='image/png')
return (response)
Strictly speaking this code has not 2 methods but 2 functions, to be considered "methods" should be members of a class.
– Sidon
For
gerar_graficos
receiverequest
as a parameter, it seems to me that she is aview
of Django. If indeed it is, it seems to me that the functionsgerar_graficos
andindex
would be executed on separate HTTP requests and this may be an aggravating in your application since the HTTP protocol, by definition, does not retain status. Anyway the question is rather vague, so I recommend that you edit it and add more details about the problem.– Woss
I edited as requested
– guilherme
So they’re actually executed on separate requests?
– Woss
they are different requests, in fact each one is to be a view, but I put the two in the same
– guilherme