How can i update my Python chart dynamically on flask?

Viewed 2042

The following chart is static, but since i'll push data every second on my Flask app, i would like to know how to make it dynamic. I tried to search for example but the only ones i found were meant for Javascript, not Python.

Would it be possible to update this chart in real time with Python? Any advice?

 def index():

        rng = pd.date_range('1/1/2011', periods=7500, freq='H')
        ts = pd.Series(np.random.randn(len(rng)), index=rng)

        graphs = [

            dict(
                data=[
                    dict(
                        x=[1, 2, 3],
                        y= arr,
                        type='scatter'
                    ),
                ],
                layout=dict(
                    title='first graph'
                )
            ) 

        ]


        ids = ['graph-{}'.format(i) for i, _ in enumerate(graphs)]
        graphJSON = json.dumps(graphs, cls=plotly.utils.PlotlyJSONEncoder)

        return render_template('index.html',
                               ids=ids,
                               graphJSON=graphJSON)
1 Answers

Investigate Asynchronous JavaScript (AJAX).

plotly attempts to eliminate the need to write JavaScript. As a result, users often end up with nowhere to turn when they need simple JavaScript functions like periodic updating.

Related