2
I have a function that returns several values, and I wanted to pass two of them out of the function, but when I try it returns that the variable outside the function has not been declared.
To illustrate in the code below, I would like the lists teste3 and teste4 of the function gerar_gráfico, to be used in the function index outside of the function gerar_graficos, remembering that it is a views of Django.
Function code:
class StorageData:
list_shared = [] #lista compartilhada
def __init__(self, *dados):
self.list_shared.extend(dados) #inclui dados na lista
def extend(self, *args):
self.list_shared.extend(args)
def reset(self):
self.list_shared = [] #reseta lista caso queira zerar ela
def gerar_graficos(request):
descricao = '01/02/2019 08:00'
descricao2 = '01/02/2019 09:00'
if request.method=='POST':
descricao = request.POST['descricao']
descricao2 = request.POST['descricao2']
conn = cx_Oracle.connect('banco')
cur = conn.cursor()
cur.execute(consulta do banco)
teste = []
teste2 = []
teste3 = []
teste4 = []
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
cur.execute(consulta do banco)
for coluna in cur:
teste2.extend(coluna)
for indice2, c in enumerate(teste2):
if indice2 >= d:
teste4.extend([c])
transfer = StorageData()
transfer.reset()
transfer.extend(teste3,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)
Code outside the function:
def index(request):
trans = StorageData()
my_list = trans.list_shared[0]
my_list2 = trans.list_shared[1]
fig = Figure()
ax = fig.add_subplot(1,1,1)
ax.plot(my_list, my_list2)
#ax.bar(x_axis, y_axis, width=width_n, color=bar_color, align='center')
ax.grid()
buf = io.BytesIO()
canvas = FigureCanvas(fig)
canvas.print_png(buf)
response=HttpResponse(buf.getvalue(), content_type='image/png')
return (response)
the two solutions work well for my question, so I will accept your answer, but what I need is not in question, unfortunately not right, thanks for the help, and if I can help more, I need to have two lists read by two different views
– guilherme
@Guilherme Use the View being a class, you can have a class attribute that would be the two lists, and you can use them independently in the two functions.
– Augusto A
Ai can have two functions in the class that generate two different templates?
– guilherme
@William so you get the two lists to be class attributes and shared between the two functions, but if the graph has any relation to the data what you can do is use a front end to generate the Graphics, putting these functions that calculate the measure in the javascript template, so it is easier depending on the application.
– Augusto A
the graph is generated from the data of the lists, but I will try to make a class with the two functions, being read each by a template, if it doesn’t work, I go to your tip of the front end, thanks brother
– guilherme