Update values of a chart using Django

1

I'm joking on a solo project to make a website to show the current coffee quote along with a line graph of the daily variation of that price as a function of time.

I get the price of coffee making "webscraping" (yes, I know it's not cool but as long as I do not find an API that does it for me I'll use webscraping, even though the site is far from airborne , I'm just running tests for now).

What I want to happen is: Given an update period (10, 15 I know how many seconds), the graph should be updated with the last value (coming from webscraping) collected and the current time.

The current code is as follows:

class CafeView(TemplateView):
    def get_context_data(self, **kwargs):
        class AppURLopener(urllib.request.FancyURLopener):  # Usado para simualar um browser e conseguir obter o html da pagina
            version = "Mozilla/5.0"
        opener = AppURLopener()
        response = opener.open('https://www.investing.com/commodities/us-coffee-c-contracts').read()
        soup = BeautifulSoup(response, 'html.parser')
        price = float(soup.find("td", {"class": "pid-8832-last"}).get_text())  # Extrai a cotação atual.
        price_var_points = float(soup.find("td", {"class": "pid-8832-pc"}).get_text())  # Extrai a variação referente à abertura da bolsa.
        context = super(CafeView, self).get_context_data(**kwargs)
        context['preco'] = price  # Passa a cotação para o template
        # Abaixo estão os parâmetros necessários para plotar o gráfico, x e y são duas listas que tem que ser preenchidas com os valores de preço/hora.
        trace1 = go.Scatter(x=x, y=y, marker={'color': 'red', 'symbol': 104, 'size': "10"},
                        mode="lines",  name='1st Trace')
        data=go.Data([trace1])
        layout=go.Layout(title="Coffee Price", xaxis={'title':'x1'}, yaxis={'title':'x2'})
        figure=go.Figure(data=data,layout=layout)
        div = opy.plot(figure, auto_open=False, output_type='div')
        context['graph'] = div
        return context
    template_name = 'cafe.html'

What I tried to do and did not work was to create 2 lists within my Class based view CafeView one to save the times and another to save the quotations, so every time a refresh on the page was pro graphic be created with values of the incremented lists, but the lists are zeroed each time the template is executed and there is no way to save the previous values. One solution I thought was to save the values in the database and each time the user refreshes the view the view uses the values stored in the database to mount the list.

I have not tried this approach yet, it probably works, I would like the opinion of someone who has more knowledge to tell me the best way to solve this problem.

NOTE: The graph is generated using the plotly library, it generates the html in the variable div and I send this html pro template in the context variable graph .

    
asked by anonymous 08.06.2017 / 17:07

1 answer

1

Yes, the idea of saving the data in the database is good, but if you do not have to persist it, since it is temporary, this strategy would consume the bank's resources unnecessarily. I would choose to create a bank in memory that in addition to avoiding unnecessary access to the bank, would probably have better performance.

To set a database in memory, see this link .

As Sqlite is "built-in" in python, it's easy to create a pure python memory bank:

sqlite3.connect(database[, timeout, detect_types, isolation_level, check_same_thread, factory, cached_statements])¶

Opens a connection to the SQLite database file database. You can use ": memory:" to open a database connection to a database that resides in RAM instead of on disk.

Specifically for django, you can do:

DATABASES = {
  'default': {
    'ENGINE': 'django.db.backends.sqlite3',
    'NAME': ':memory:',
  }
}
    
08.06.2017 / 19:38