Muzamal Raza Soomro
Muzamal Raza Soomro

Reputation: 31

How to receive a form input in a python script in django?

what i nee is: On a html form when user click the submit button the value of that button to be received as a python variable so that i can use it in my script.

What i want to do is plot some plotly plots by using the file uploaded by the user.

I've tried to send the data through the post request and receive it in my view function and render it on html page but what i want is that data to be saved as a global or any type of variable that can be used in any python script in my app.

URL.py

path("event/",views.event,name="event"),

Views.py:

def event(request):
    context={}
    file= request.POST.get('file',None)
    context['file']=file
    return render(request,'main/event.html',context)

Following is my code of plot.py

from plotly.offline import plot
import plotly.graph_objs as go
import pandas as pd

def get_simple_candlestick():

    df = pd.read_csv("FILE PATH RECEIVED FROM FORM")
    df.head()

    data = [
        go.Bar(
            x=df['x'], # assign x as the dataframe column 'x'
            y=df['y']
        )
    ]
    layout = go.Layout(
        title='Plot Title',
     xaxis=dict(
        autorange=True
        ),
     yaxis=dict(
        autorange=True
        )
     )
    plot_data = data
    figure = go.Figure(data=plot_data, layout=layout)
    plot_div = plot(figure, output_type='div', include_plotlyjs=False)
    return plot_div

what i expect is that the data to be read by .read_csv() function should be the the one that user uploaded and selected from many of the files he uploaded.

Upvotes: 2

Views: 216

Answers (1)

Muzamal Raza Soomro
Muzamal Raza Soomro

Reputation: 31

I did it by simply making global variable.

Upvotes: 1

Related Questions