SHARE
TWEET

Untitled

angelorcc Feb 14th, 2020 72 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import dash
  2. from dash.dependencies import Input, Output
  3. import dash_core_components as dcc
  4. import dash_html_components as html
  5. import plotly
  6. import plotly.graph_objs as go
  7. from collections import deque
  8. import pandas as pd
  9. import pyodbc
  10.  
  11.  
  12. def connectSQLServer(driver, server, db):
  13.     connSQLServer = pyodbc.connect(
  14.         'DRIVER={;/usr/local/lib/psqlodbcw.so'
  15.         'SERVER=+localhost+;'
  16.         'DATABASE=+CompanyA+;'
  17.         'OPTION=4;'
  18.         'autocommit=True;'
  19.     )
  20.     return connSQLServer
  21.  
  22.  
  23. name_title = 'Import from SQL Server'
  24. app = dash.Dash(__name__)
  25.  
  26. app.layout = html.Div(children=[
  27.        
  28.     html.H1(children='Read near real-time data from SQL Server on Scatterplot '),
  29.      dcc.Graph(
  30.         id='example-graph',animate=True),dcc.Interval(id='graph-update',interval=1*500),])
  31.  
  32.    
  33. @app.callback(Output('example-graph', 'figure'),
  34.        
  35.               [Input('graph-update', 'interval')])
  36.  
  37.  
  38. def update_graph_scatter(Graph):
  39.  
  40.     dataSQL = [] #set an empty list
  41.     X = deque(maxlen=10)    
  42.     Y = deque(maxlen=10)
  43.  
  44.     sql_conn = connectSQLServer('/usr/local/lib/psqlodbcw.so','localhost:', 'CompanyA.dbo')
  45.     cursor = sql_conn.cursor()
  46.     cursor.execute("SELECT * FROM departments")
  47.     rows = cursor.fetchall()
  48.     for row in rows:
  49.         dataSQL.append(list(row))
  50.         labels = ['num','id']
  51.         df = pd.DataFrame.from_records(dataSQL, columns=labels)
  52.         X = df['id']
  53.         Y = df['num']
  54.        
  55.  
  56.     data = plotly.graph_objs.Scatter(
  57.             x=list(X),
  58.             y=list(Y),
  59.             name='Scatter',
  60.             mode= 'lines+markers'
  61.             )
  62.  
  63.     return {'data': [data],'layout' : go.Layout(xaxis=dict(range=[min(X),max(X)]),
  64.                                                 yaxis=dict(range=[min(Y),max(Y)]),)}
  65.  
  66.  
  67.  
  68. if __name__=="__main__":
  69.         app.run_server(port=5000, debug=True)
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top