0

I have a database with population data. I want to create a page that shows a figure of this data (with for example matplotlib). How and where would I store the figures which I create with matplotlib?

Example data:

year | people
2010 | 100
2011 | 110
2012 | 110
2013 | 114
2014 | 124
2015 | 154
2016 | 143
2017 | 112

In my view I already have collected the variable, year and people. I would normally do something like this:

plt.plot(year, people)
plt.savefig('figure.svg')

The question is where/how would I store this and present it to the user? Should I store this in a media directory? I do not want to store the file forever cause then diskspace would quickly fill up with graphs which are only used once.

Evhz
  • 8,852
  • 9
  • 51
  • 69
Yorian
  • 2,002
  • 5
  • 34
  • 60
  • you can use matplotlib with django https://stackoverflow.com/questions/30531990/matplotlib-into-a-django-template. anyway, why not store it in a db – Jason May 16 '18 at 11:36

2 Answers2

0

Try saving the images in cache. Look at django-sorl-thumbnail for how to try that.

Vinay P
  • 617
  • 3
  • 13
0

You have several options:

One is to store figures under a specific folder inside the static folder you configured. (Have in mind that django has a good solution for public assets and you can configure your webserver to directly serve those files).
Have a look at this question about serving those files using nginx.

Having this config in settings.py:

STATIC_ROOT = os.path.dirname(BASE_DIR) + '/static/'

It is possible to access files stored in:

/static/figures

http://your.domain.com/static/figures/figure.svg

In case that is not enough, use any Object Storage provider, here there are few of them:

Have a look at this django documentation on static files deployment. It is very helpful.

Evhz
  • 8,852
  • 9
  • 51
  • 69