Reputation: 11
File "C:\Users\Administrator\Desktop\cms项目\intelcms\plotly_django_tutorial\home\urls.py", line 5, in from home.dash_apps.finished_apps import secondexample File "C:\Users\Administrator\Desktop\cms项目\intelcms\plotly_django_tutorial\home\dash_apps\finished_apps\secondexample.py", line 48, in def update_metrics(n): File "C:\Users\Administrator\Desktop\cms项目\intelcms\venv\lib\site-packages\django_plotly_dash\dash_wrapper.py", line 345, in wrap_func func.expanded = DjangoDash.get_expanded_arguments(func, inputs, state) File "C:\Users\Administrator\Desktop\cms项目\intelcms\venv\lib\site-packages\django_plotly_dash\dash_wrapper.py", line 305, in get_expanded_arguments n_dash_parameters = len(inputs or []) + len(state or []) TypeError: object of type 'Input' has no len()
Unable to find stateless DjangoApp called SecondExample
I want to create a django live update app. Here is the bug report, when i use DjangoDash to create an app the first bug showed up, it says that the input has no len(), i dont quite understand how this problem shows up.
when i use dash. Dash to create an Django App the second bug showed up,but in the solo test the app is created, it is just not integrated in Django app. Blow is the full code. The difference is in line 22 and 23. Hope you can help me solve this Problem. Thanks a lot.
1.
2. import dash_core_components as dcc
3. import time
4. import datetime
5. import dash
6. import dash_html_components as html
7. from dash.dependencies import Input, Output
8. import plotly
9. import plotly.graph_objs as go
10. import plotly.express as px
11. from django_plotly_dash import DjangoDash
12. from home.dash_apps.finished_apps import opc as op
13.
14. nodeDo = "ns=2;s=Demo.Dynamic.Scalar.Double"
15. nodeFl = "ns=2;s=Demo.Dynamic.Scalar.Float"
16. nodeIn = "ns=2;s=Demo.Dynamic.Scalar.Int32"
17. ServerName = 'opc.tcp://localhost:48010'
18. opcObject = op.GetDataFromOpcServer(ServerName=ServerName, NodeID=nodeDo)
19.
20. external_stylesheets = ['https://codepen.io/chriddyp/pen/bWLwgP.css']
21.
22. #app = DjangoDash('SecondExample', external_stylesheets=external_stylesheets)
23. app = dash.Dash('SecondExample', external_stylesheets=external_stylesheets)
24.
25. num = 0
26. app.layout = html.Div(
27. html.Div([
28. html.H1('Square Root Slider Graph'),
29. html.Div(id='live-update-text'),
30. dcc.Graph(id='live-update-graph'),
31. dcc.Interval(
32. id='interval-component',
33. disabled=False,
34. interval=1*1000, # in milliseconds
35. n_intervals=0,
36. max_intervals=100
37. )
38. ])
39. )
40.
41. @app.callback(Output('live-update-text', 'children'),
42. Input('interval-component', 'n_intervals'))
43. def update_metrics(n):
44. lon = op.GetDataFromOpcServer(ServerName=ServerName, NodeID=nodeDo)
45. lat = op.GetDataFromOpcServer(ServerName=ServerName, NodeID=nodeFl)
46. alt = op.GetDataFromOpcServer(ServerName=ServerName, NodeID=nodeIn)
47. style = {'padding': '5px', 'fontSize': '16px'}
48. return [
49. html.Span('Longitude: {0:.2f}'.format(lon), style=style),
50. html.Span('Latitude: {0:.2f}'.format(lat), style=style),
51. html.Span('Altitude: {0:0.2f}'.format(alt), style=style)
52. ]
53.
54.
55. # Multiple components can update everytime interval gets fired.
56. @app.callback(Output('live-update-graph', 'figure'),
57. Input('interval-component', 'n_intervals'))
58. def update_graph_live(n):
59. data = {
60. 'time': [],
61. 'Latitude': [],
62. 'Longitude': [],
63. 'Altitude': []
64. }
65.
66. # Collect some data
67. time = datetime.datetime.now() - datetime.timedelta(seconds=2000)
68. lon = op.GetDataFromOpcServer(ServerName=ServerName, NodeID=nodeDo)
69. lat = op.GetDataFromOpcServer(ServerName=ServerName, NodeID=nodeFl)
70. alt = op.GetDataFromOpcServer(ServerName=ServerName, NodeID=nodeIn)
71. data['Longitude'].append(lon)
72. data['Latitude'].append(lat)
73. data['Altitude'].append(alt)
74. data['time'].append(time)
75.
76. # Create the graph with subplots
77. fig = plotly.subplots.make_subplots(rows=3, cols=1, vertical_spacing=0.2)
78. fig['layout']['margin'] = {
79. 'l': 30, 'r': 10, 'b': 30, 't': 10
80. }
81. fig['layout']['legend'] = {'x': 0, 'y': 1, 'xanchor': 'left'}
82.
83. fig.append_trace({
84. 'x': data['time'],
85. 'y': data['Altitude'],
86. 'name': 'Altitude',
87. 'mode': 'lines+markers',
88. 'type': 'scatter'
89. }, 1, 1)
90. fig.append_trace({
91. 'x': data['time'],
92. 'y': data['Latitude'],
93. 'text': data['time'],
94. 'name': 'Latitude',
95. 'mode': 'lines+markers',
96. 'type': 'scatter'
97. }, 2, 1)
98. fig.append_trace({
99. 'x': data['time'],
100. 'y': data['Longitude'],
101. 'text': data['time'],
102. 'name': 'Longitude',
103. 'mode': 'lines+markers',
104. 'type': 'scatter'
105. }, 3, 1)
106.
107. return fig
108.
109. if __name__ == '__main__':
110. app.run_server(debug=True)
Upvotes: 1
Views: 513
Reputation: 6616
You need to wrap your Input
s in lists, like this:
41. @app.callback(Output('live-update-text', 'children'),
42. [Input('interval-component', 'n_intervals')])
43. def update_metrics(n):
# func code...
If you fix them both, then that should resolve your issue.
Upvotes: 1