Вот наш view.py:
Код: Выделить всё
def simple_upload(request):
if request.method == 'POST' and request.FILES['file']:
myfile = request.FILES['file']
fs = FileSystemStorage()
filename = fs.save(myfile.name, myfile)
# this is the function that returns image url
forecast, clean_df, graph_url = data_processing(myfile, filename)
forecast = forecast.reset_index()
cols_fc = forecast.columns.tolist()
forecast[cols_fc[0]] = forecast[cols_fc[0]].dt.date
forecast = forecast.values.tolist()
clean_df = clean_df.reset_index()
cols_df = clean_df.columns.tolist()
clean_df[cols_df[0]] = clean_df[cols_df[0]].dt.date
clean_df = clean_df.values.tolist()
context = {
'filename': filename,
'cols_fc': cols_fc,
'forecast': forecast,
'cols_df': cols_df,
'clean_df': clean_df,
'graph_url': graph_url,
}
return render(request, 'project/results.html', context)
return render(request,'project/example.html')
Код: Выделить всё
def data_processing(filename):
df = pd.read_excel(filename)
cols = df.columns.values.tolist()
df = df.set_index(cols[0])
df = df[[cols[1]]]
clean_df = process_missing(df)
integr = stationate_data(clean_df)
model_param = search_optimal_arima(clean_df, integr)
model = sm.tsa.arima.ARIMA(clean_df, order=model_param, freq='MS')
results = model.fit()
forecast = get_forecast(results, clean_df, cols[1])
graph_url = make_graph(forecast, clean_df, cols, filename)
return forecast, clean_df, graph_url
Код: Выделить всё
def make_graph(forecast, df, cols, filename):
plt.figure(figsize=(16,4))
plt.plot(df, label="Реальные значения", color="blue")
plt.plot(forecast, label='Прогноз', color='red')
plt.title(filename)
plt.xlabel(cols[1])
plt.ylabel(cols[0])
plt.legend()
figname = (f'{filename}_forecast.png')
plt.savefig(os.path.join(settings.MEDIA_ROOT, 'graphs/', figname))
return figname
Код: Выделить всё
{% load static %}
{{ filename }}
------------------------------------------------------
{{ graph_url }}
[img]{% static [/img]
------------ VALUES -----------
{{ cols_df }}
{% for c in clean_df %}
{{ c.0 }} || {{ c.1 }}
{% endfor %}
------------- FORECAST -------------
{{ cols_fc }}
{% for f in forecast %}
{{ f.0 }} || {{ f.1 }}
{% endfor %}
Код: Выделить всё
STATIC_URL = 'static/'
STATIC_ROOT = os.path.join(BASE_DIR, STATIC_URL)
MEDIA_URL = 'media/'
MEDIA_ROOT = os.path.join(STATIC_ROOT, MEDIA_URL)
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'project/static/static_files/'),
os.path.join(BASE_DIR, 'static/media/graphs/')
)
- Записываем полный URL-адрес пути/к/{{ url }} и со статическим каталогом {% static '{{ url }}'%, но они не работают.
Код: Выделить всё
graph_urlПодробнее здесь: https://stackoverflow.com/questions/784 ... -in-django