#How to display dataframe calculated in a view?

1 messages · Page 1 of 1 (latest)

balmy belfry
#

With a form i process data from a csv file in a view. Now i want to display this dataframe in a table. I have this dataframe 'store' in one view but i have no idea how to display it.

here is my view function: i take input from form and use it to process numbers in a df from a csv. Now this new df i want to display to the user but i have no idea what to do with this new df 'carsnorm'

'''
def prioinput(request):
form = forms.InputParams(request.POST, request.FILES)
if form.is_valid():
carsnorm.sort_values(by=['mean'],ascending=0)
return render(request, 'cars4you/prioinput.html',{'form':form})
'''

strange sinew
#

The simplest thing you could probably do is {{ df.to_html|safe }} You'll need to pass df to the template.

But you'll need to style this. Otherwise, you can just treat the dataframe like any other "List of things" and just loop through the rows and display what you need.

P.S. You're using POST request, you technically should be redirecting, not using render. Though you will use the dataframe in this case.

Another thing, you're using sort_values(...) which I don't believe is mutating the dataframe, you'll need to assign it to a variable.

#

I suggest you think about the full roundtrip from the browser to the server and back, and decide on what you plan on doing/showing.

balmy belfry
#

Ok, i see that i can use the return HttpResponse.
How should i redirect? Will the data from this view be still available if I redirect? I dont want this data to save anywhere its only for the user for now.

#

Could you also explain where to style it? You mean when i redirect it should be to a url that is styled right? For now i don't know how to redirect so i'm doing it now in the same view

#

I don't know how to save this dataframe so that i can 'deal' with it. So far i can only do it in one view because i dont know how to pass it somewhere else

strange sinew
#

Is there a specific reason you're creating a "DataFrame" out of the input?

Usually, the data that's sent from the user is stored in the database so that i can be used further down the line.

DataFrames are used temporarily to "do something" with the data.

#

The main issue here is that the user is POST-ing the data, the general practice here is to redirect elsewhere after the action has been successful, otherwise you'd render the result.

I under the "it's only for the user for now", but that doesn't particularly change how things are supposed to work.

#

redirecting before "saving" this data implies the loss of said data.