Issue
I did this web app in Flask with a friend and I had the following form to get X and Y for any kind of plot from an uploaded CSV. The code is below:
<form action="{{url_for('plot')}}" method="POST" enctype="multipart/form-data">
<div class="form-group"><label>Select X Axis</label><select class="form-control" name="x_axis" required>
<option value=""></option>
{% for column in cols %}
<option value="{{column|string}}">{{column}}</option>
{% endfor %}
</select></div>
<div class="form-group"><label>Select Y Axis</label><select class="form-control" name="y_axis" required>
<option value=""></option>
{% for column in cols %}
<option value="{{column|string}}">{{column}}</option>
{% endfor %}
</select></div>
<div class="form-group"><label>Plot Type</label><select class="form-control" name="plot_type" required>
<option value=""></option>
<option value="scatter">Scatter Plot</option>
<option value="line">Line Plot</option>
<option value="bar">Bar Plot</option>
<option value="sunburst">Sunburst Plot</option>
<option value="corelation">Correlation Matrix</option>
<option value="table">Table</option>
</select><br><br>
<div class="form-group"><button class="btn btn-primary btn-block" type="submit">Plot</button></div>
</form>
The previous page is to upload the CSV and we get the column names from that for these options.
I get a KeyError for any column when hitting submit but it works fine locally.
Almost like
KeyError: "Column"
I have tried removing the string in {{column|string}} to no avail.
Areas where I feel the error can be:
- Saving the CSV
I save the CSV with the following code:
def plot_choose():
f = request.files['csv']
if f.filename.split('.')[1] == 'csv':
f.save(secure_filename(f.filename))
else:
return "<h1>Incorrect File Format</h1>"
global df
df = pd.read_csv(f.filename)
df = df.dropna(how = "any")
return render_template('plot.html', cols=list(df.columns), tables = [df.head().to_html(classes='data')])
I declared df at the start as just pd.Dataframe({}) and called global here. The error existed however even without doing this bit actually.
The form code
I mentioned the form code earlier. I think maybe the frontend is not passing the column name properly I don't know.The code to get columns
The code to get the columns from the form is below:
x_ax = str(request.form['x_axis'])
y_ax = str(request.form['y_axis'])
plot = str(request.form['plot_type'])
The funny thing here is that it results in the same error even with a CSV that is stored in the server too.
Any other clarifications, I will be happy to address.
Solution
I kept trying to use it in Heroku but it was of no use. I switched to PythonAnywhere and now the error does not pop up.
Answered By - Charles Samuel
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.