trench
trench

Reputation: 5355

Flask - output multiple dataframes and graphs?

I set up a small site that allows a user to upload an Excel file which I process and load into a database. As a sort of 'confirmation' window, I quickly read the raw data they upload and do some aggregation and formatting with pandas. It is pretty neat. I tried to create several dataframes and even a matplotlib graph but I wasn't able to tie several outputs all together in one view. (in my return statement, I tried to return several items separated by commas but had an error)

def to_html(df, caption):
    html = (df.style
              .format(format_numbers)
              .set_table_styles(styles)
              .set_properties(**{'width': '100', 'margin-left': 'auto', 'margin-right': 'auto', 'margin': '0px auto', "align": "center", "text-align": "center"})
              .set_caption(caption)
              .render())
    return html


@app.route('/upload', methods=['POST'])
def upload():
    file = request.files['file']
    if file and allowed_file(file.filename):
        filename = secure_filename(file.filename)
        file.save(filename)
        df = pd.read_excel(filename, index_col=None)
        daily = pd.pivot_table(df, index=['date', 'dimension'], columns=['other'], values=['stuff'], aggfunc=[np.sum], fill_value=0)
        daily.reset_index(inplace=True)
#to_html is a function I made which uses df.style from pandas, so this outputs a rendered pandas dataframe
        return to_html(monthly, 'Your file was successfully uploaded. Here is a summary of the data:'),  to_html(daily, caption='Daily details:')
    else:
        return 'File type is not supported.'

My log files on pythonanywhere are only showing, but the error is 502:

2017-01-12 17:54:13,627 :OSError: write error
2017-01-12 17:54:13,628 :Error running WSGI application
2017-01-12 17:54:13,628 :GeneratorExit

User uploads a file, I read it with pandas and do some aggregations/calculations/graphing. How can I best display these graphs and pandas DataFrames (HTML) in a sort of quick dashboard format?

The file they are uploading is a standard template and it is under 1MB. The data is time series so it makes a lot of sense to look at it daily, monthly, maybe per weekday, perhaps add some quick forecasting or expectations, etc.

Upvotes: 1

Views: 1537

Answers (1)

Hng
Hng

Reputation: 148

You should plot your data using/with/at front-end (using, d3, highcharts, cytoscape). Use flask APIs to return data only. In particular, you should return, say, hourly-data, daily_data, weekly_data as using jsonify function fron flask.

>>> def somefunc(params):
>>>    #.... some logic to get generate hourly, daily and weekly
>>>    data_dict = {}
>>>    data_dict["daily_data_to_plot"] = daily_data
>>>    data_dict["weekly_data_to_plot"] = weekly_data
>>>    data_dict["hourly_data_to_plot"] = hourly_data
>>>    return jsonify(data_dict)

Then you try to read this data with ruby/js to display at the front end.

Upvotes: 1

Related Questions