Python in real time

I would like to make a graph in real time on plotly with a Python program

import plotly
import random
from plotly.graph_objs import Scatter, Layout
valeurx = []
valeury = []
i=0
while (i<10):
i=i+1
valeurx.append(i)
valeury.append(random.randint(0,50))

plotly.offline.plot({
“data”: [
Scatter(x=valeurx, y= valeury)
],
“layout”: Layout(
title=“evolution de la consigne et distance”
)
})

I tested this program but the problem is that it only appears at the end of the program otherwise it opens me a window by data and it is not on the internet

You need to create a Dash app, not just a plotly graph, look at the Dash documentation: https://dash.plot.ly/ and specifically live updates: https://dash.plot.ly/live-updates

I’ve created this code in the past of a very simple example of live updating a graph:

import random

import dash
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input, Output, State
from dash.exceptions import PreventUpdate


external_stylesheets = ['https://codepen.io/chriddyp/pen/bWLwgP.css']
app = dash.Dash(__name__, external_stylesheets=external_stylesheets)


app.layout = html.Div(children=[
    html.H1(children='Hello World!', id='first'),
    dcc.Interval(id='timer', interval=1000),
    html.Div(id='dummy'),
    dcc.Graph(
            id='example-graph',
            figure={
                'data': [
                    {'x': [1, 2, 3],
                     'y': [4, 1, 2],
                     'type': 'bar', 'name': 'SF'},
                    {'x': [1, 2, 3],
                     'y': [2, 4, 5],
                     'type': 'bar', 'name': 'Montréal'},
                ],
                'layout': {
                    'title': 'Dash Data Visualization'
                }
            }
        )
])

@app.callback(output=Output('example-graph', 'figure'),
              inputs=[Input('timer', 'n_intervals')])
def update_graph(n_clicks):
    return {
                'data': [
                    {'x': [1, 2, 3],
                     'y': [random.randint(0, 10) for x in range(3)],
                     'type': 'bar', 'name': 'SF'},
                    {'x': [1, 2, 3],
                     'y': [random.randint(0, 10) for x in range(3)],
                     'type': 'bar', 'name': 'Montréal'},
                ],
                'layout': {
                    'title': 'Dash Data Visualization'
                }
            }


if __name__ == '__main__':
    app.run_server(debug=True)

It’s a modified version of the tutorial bar graph with live updates built in to it.

1 Like

thank you but when I started a program there was a long error message the same as when I tried the one from the dash guide:

Running on http://127.0.0.1:8050/
Debugger PIN: 828-777-804
Traceback (most recent call last):
File “C:/Users/matth/AppData/Local/Programs/Python/Python37-32/test2.py”, line 109, in
app.run_server(debug=True)
File “C:\Users\matth\AppData\Local\Programs\Python\Python37-32\lib\site-packages\dash\dash.py”, line 1318, in run_server
**flask_run_options)
File “C:\Users\matth\AppData\Local\Programs\Python\Python37-32\lib\site-packages\flask\app.py”, line 938, in run
cli.show_server_banner(self.env, self.debug, self.name, False)
File “C:\Users\matth\AppData\Local\Programs\Python\Python37-32\lib\site-packages\flask\cli.py”, line 629, in show_server_banner
click.echo(message)
File “C:\Users\matth\AppData\Local\Programs\Python\Python37-32\lib\site-packages\click\utils.py”, line 218, in echo
file = _default_text_stdout()
File “C:\Users\matth\AppData\Local\Programs\Python\Python37-32\lib\site-packages\click_compat.py”, line 675, in func
rv = wrapper_func()
File “C:\Users\matth\AppData\Local\Programs\Python\Python37-32\lib\site-packages\click_compat.py”, line 436, in get_text_stdout
rv = _get_windows_console_stream(sys.stdout, encoding, errors)
File “C:\Users\matth\AppData\Local\Programs\Python\Python37-32\lib\site-packages\click_winconsole.py”, line 295, in _get_windows_console_stream
func = _stream_factories.get(f.fileno())
io.UnsupportedOperation: fileno

can you tell me where I wrong?

It’s a weird error but I think it’s because you’ve created your Python file in a location that you need system privileges to have lock access to the file.

Either create your python script somewhere else or try running it without debug=True which shouldn’t then need lock access to the file, e.g.:

app.run_server()

I recreated the script in my documents and even without the “debug = True” there is the error

I Googled your error and found this trail of Stackoverflow questions:

Are you trying to run this in IDLE Python IDE or some other similar tool? If so try running it from the command line:

python test2.py

thank you very much it works well it was enough to activate it in the command prompt with the command:
for example if you have put your file in your documents take the commande:
python C:\Users\matth\Documents\9-PYTHON\test2.py