B
B
blackbb2016-04-20 14:14:17
Django
blackbb, 2016-04-20 14:14:17

How to display two functions in one in views.py?

It needs to be all on one page. Now each function on a separate page produces a solution.
k_means and ier function:

def k_means(request):
    import django
    from gigaapp.k_means_app import main
    canvas=main()
    response=django.http.HttpResponse(content_type='image/png')
    canvas.print_png(response)
    return response


def ier(request):
    import django
    import xlrd
    import matplotlib
    matplotlib.use('Agg')
    from scipy.spatial.distance import pdist
    from scipy.cluster.hierarchy import linkage, dendrogram
    from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas
    from matplotlib import pyplot as plt
    
    rb = xlrd.open_workbook(os.path.dirname(__file__)+'/data.xlsx')
    sheet = rb.sheet_by_index(0)
    x = [sheet.row_values(rownum) for rownum in range(sheet.nrows)]
    del (x[0])
    i=0
    n=len(x)
    while i<n:
        del x[i][0]
        i=i+1
    fig=plt.figure(figsize=(15, 10))
    ax=fig.gca()
    ax.yaxis.set_visible(False)
    plt.title('Clustering')
    plt.xlabel('Index')
    y=pdist(x)
    data=linkage(y)
    dendrogram(data,color_threshold=1,show_contracted=True)
    canvas=FigureCanvas(fig)
    response=django.http.HttpResponse(content_type='image/png')
    canvas.print_png(response)
    return response

def current_ier(request):
   return ier(request)

def current_kmeans(request):
    return k_means(request)

If doing so
def ier_kmeans(request):
    a = k_means(request)
    b = ier(request)
    return render(request, 'some_template.html', {'a': a, 'b': b})

then it throws an error
'utf8' codec can't decode byte 0x89 in position 27: invalid start byte. You passed in <HttpResponse status_code=200, "image/png"> (<class 'django.http.response.HttpResponse'>)

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Sergey Gornostaev, 2016-04-20
@sergey-gornostaev

View should return HttpResponse

def ier_kmeans(request):
    a = k_means(request)
    b = ier(request)
    return render(request, 'some_template.html', {'a': a, 'b': b})

V
Vladimir Kuts, 2016-04-20
@fox_12

The topic is not open.
It is not clear what data you return in the k_means and ier functions .
And what do you want to get back?
If you return objects of type request in the first and second functions , just pass it to the second function. And return the result of the second function. If you return some variables, then return the context to the answer, as Sergey Gornostaev advises

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question