Plotly-Dash with nidaqmx, name 'task' is not defined 错误
Plotly-Dash with nidaqmx, name 'task' is not defined error
我正在使用 python 到 windows,我正在尝试使用加速度计从 NI 9234 读取数据。我希望远程读取这些数据,为此我正在使用 Dash。而对于 NI 部分,我试图使用 nidaqmx,使用这个 example.
我的 dash 程序现在很简单,它在没有 nidaqmx 部分的情况下工作(做一些非常简单的事情),而 nidaqmx 程序成功地从传感器读取数据。如果我尝试将它们合并在一起,则会出现以下错误:
这是我的脚本:
import dash
import dash_daq as daq
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input, Output
import plotly.graph_objects as go
from dash.exceptions import PreventUpdate
import nidaqmx
from nidaqmx.constants import AcquisitionType
import numpy as np
external_stylesheets = ['https://codepen.io/chriddyp/pen/bWLwgP.css']
app = dash.Dash(__name__, external_stylesheets=external_stylesheets)
# some color to this
colors = {
'background': '#111111',
'text': '#7FDBFF'
}
#Initial conditions to have something in the graph
trace_1 = go.Scatter(
x = [],
y = []
)
layout = go.Layout(title = 'Título de gráfica')
fig = go.Figure(data = [trace_1], layout = layout)
app.layout = html.Div(style = {'backgroundColor': colors['background']},
children = [
# H3 es para marcar el título, es el mediano que los demás, H1 es súper grande.
html.H3(
children = 'Ejemplo fácil para chequear botones-gráficas-menus',
style = {
'textAlign': 'center',
'color': colors['text']
}
),
# Div parece que divide éste párrafo abajo de lo anterior
html.Div(
id= 'respuesta',
children= 'Clickea el botón',
style = {
'textAlign': 'center',
'color': colors['text']
}
),
# Se define el botón:
daq.BooleanSwitch(
id = 'Swtc_1',
label = 'Capturar',
on = False
),
html.Div(
[
html.Br(),
html.Br(),
html.Label(
['Elija algo'],
style = {
'font-weight': 'bold',
'text-align': 'center',
'color': colors['text']
}
),
dcc.Dropdown(
id = 'f_muestreo',
options = [
{'label' : '2048', 'value':'2048'},
{'label' : '2560', 'value':'2560'},
{'label' : '3200', 'value':'3200'},
{'label' : '5120', 'value':'5120'},
{'label' : '6400', 'value':'6400'},
{'label' : '10240', 'value':'10240'},
{'label' : '12800', 'value':'12800'},
{'label' : '25600', 'value':'25600'}
],
value = '2048',
multi = False,
disabled = False,
persistence = 'string',
persistence_type = 'session'
),
html.Br(),
dcc.Dropdown(
id = 'muestras',
options = [
{'label' : '2048', 'value':'2048'},
{'label' : '4096', 'value':'4096'},
{'label' : '8192', 'value':'8192'},
{'label' : '16384', 'value':'16384'},
{'label' : '32768', 'value':'32768'},
{'label' : '65536', 'value':'65536'},
{'label' : '131072', 'value':'131072'}
],
value = '2048',
multi = False,
disabled = False,
persistence = 'string',
persistence_type = 'session'
)
],className = 'three columns'
),
# graph
html.Div(
[
dcc.Graph(id = 'plot_id', figure = fig)
],className = 'eight columns'
)
]
)
# interaction
@app.callback(
Output('plot_id', 'figure'),
[Input('Swtc_1', 'on'),
Input('f_muestreo', 'value'),
Input('muestras', 'value')]
)
def update_output(on, value_1, value_2):
if on is True:
sample_rate = float(value_1)
samples_to_acq = float(value_2)
wait_time = samples_to_acq/sample_rate
#Name and channel of my NI 9234
channel_name = 'cDAQBAYO1Mod1/ai0'
#Not using the trig yet
#trig_name = 'cDAQBAYO1Mod1/ai1'
cont_mode = AcquisitionType.CONTINUOUS
units_g = nidaqmx.constants.AccelUnits.G
# Create accelerometer channel and configure sample clock and trigger specs
task.ai_channels.add_ai_accel_chan(channel_name, units = units_g)
task.timing.cfg_samp_clk_timing(sample_rate, sample_mode = cont_mode, samps_per_chan=samples_to_acq)
#task.triggers.start_trigger.cfg_dig_edge_start_trig(trigger_source = trig_name)
# Reading data from sensor and generating time data with numpy
ydata = task.read(number_of_samples_per_channel=samples_to_acq)
xdata = np.linspace(0, wait_time,samples_to_acq)
trace_1 = go.Scatter(
x = list(xdata),
y = list(ydata)
)
layout = go.Layout(title = 'Oscilograma')
fig = go.Figure(data = [trace_1], layout = layout)
return (fig)
else:
raise PreventUpdate
if __name__ == '__main__':
app.run_server(port=3040, debug=True)
我在想也许 dash 不支持 nidaqmx,如果是这种情况,一个可能的解决方案是使用两个脚本:一个用于读取传感器,另一个在 dash 中用于显示信息到用户...但是,出于澄清目的,我试图将所有内容都放在一个脚本中。
到处搜索,但找不到在 dash 中实现的 nidaqmx。非常感谢任何帮助。
谢谢,dm2!就像你说的,我没有声明任务,我也 运行 遇到了一个我需要进一步研究的 ctypes 问题,但现在我将声明我的值作为 int。我发布解决方案是因为我猜世界上到处都是像我这样的新手:
import dash
import dash_daq as daq
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input, Output
import plotly.graph_objects as go
from dash.exceptions import PreventUpdate
import nidaqmx
from nidaqmx.constants import AcquisitionType
import numpy as np
external_stylesheets = ['https://codepen.io/chriddyp/pen/bWLwgP.css']
app = dash.Dash(__name__, external_stylesheets=external_stylesheets)
# some color to this
colors = {
'background': '#111111',
'text': '#7FDBFF'
}
#Initial conditions to have something in the graph
trace_1 = go.Scatter(
x = [],
y = []
)
layout = go.Layout(title = 'Título de gráfica')
fig = go.Figure(data = [trace_1], layout = layout)
app.layout = html.Div(style = {'backgroundColor': colors['background']},
children = [
# H3 es para marcar el título, es el mediano que los demás, H1 es súper grande.
html.H3(
children = 'Ejemplo fácil para chequear botones-gráficas-menus',
style = {
'textAlign': 'center',
'color': colors['text']
}
),
# Div parece que divide éste párrafo abajo de lo anterior
html.Div(
id= 'respuesta',
children= 'Clickea el botón',
style = {
'textAlign': 'center',
'color': colors['text']
}
),
# Se define el botón:
daq.BooleanSwitch(
id = 'Swtc_1',
label = 'Capturar',
on = False
),
html.Div(
[
html.Br(),
html.Br(),
html.Label(
['Elija algo'],
style = {
'font-weight': 'bold',
'text-align': 'center',
'color': colors['text']
}
),
dcc.Dropdown(
id = 'f_muestreo',
options = [
{'label' : '2048', 'value':'2048'},
{'label' : '2560', 'value':'2560'},
{'label' : '3200', 'value':'3200'},
{'label' : '5120', 'value':'5120'},
{'label' : '6400', 'value':'6400'},
{'label' : '10240', 'value':'10240'},
{'label' : '12800', 'value':'12800'},
{'label' : '25600', 'value':'25600'}
],
value = '2048',
multi = False,
disabled = False,
persistence = 'string',
persistence_type = 'session'
),
html.Br(),
dcc.Dropdown(
id = 'muestras',
options = [
{'label' : '2048', 'value':'2048'},
{'label' : '4096', 'value':'4096'},
{'label' : '8192', 'value':'8192'},
{'label' : '16384', 'value':'16384'},
{'label' : '32768', 'value':'32768'},
{'label' : '65536', 'value':'65536'},
{'label' : '131072', 'value':'131072'}
],
value = '2048',
multi = False,
disabled = False,
persistence = 'string',
persistence_type = 'session'
)
],className = 'three columns'
),
# graph
html.Div(
[
dcc.Graph(id = 'plot_id', figure = fig)
],className = 'eight columns'
)
]
)
# interaction
@app.callback(
Output('plot_id', 'figure'),
[Input('Swtc_1', 'on'),
Input('f_muestreo', 'value'),
Input('muestras', 'value')]
)
def update_output(on, value_1, value_2):
if on is True:
sample_rate = int(value_1)
samples_to_acq = int(value_2)
wait_time = samples_to_acq/sample_rate
#Name and channel of my NI 9234
channel_name = 'cDAQBAYO1Mod1/ai0'
#Not using the trig yet
#trig_name = 'cDAQBAYO1Mod1/ai1'
cont_mode = AcquisitionType.CONTINUOUS
units_g = nidaqmx.constants.AccelUnits.G
with nidaqmx.Task() as task:
# Create accelerometer channel and configure sample clock and trigger specs
task.ai_channels.add_ai_accel_chan(channel_name, units = units_g)
task.timing.cfg_samp_clk_timing(sample_rate, sample_mode = cont_mode, samps_per_chan=samples_to_acq)
#task.triggers.start_trigger.cfg_dig_edge_start_trig(trigger_source = trig_name)
# Reading data from sensor and generating time data with numpy
ydata = task.read(number_of_samples_per_channel=samples_to_acq)
xdata = np.linspace(0, wait_time,samples_to_acq)
trace_1 = go.Scatter(
x = list(xdata),
y = list(ydata)
)
layout = go.Layout(title = 'Oscilograma')
fig = go.Figure(data = [trace_1], layout = layout)
return (fig)
else:
raise PreventUpdate
if __name__ == '__main__':
app.run_server(port=3040, debug=True)
传感器正在读取数据!!
我正在使用 python 到 windows,我正在尝试使用加速度计从 NI 9234 读取数据。我希望远程读取这些数据,为此我正在使用 Dash。而对于 NI 部分,我试图使用 nidaqmx,使用这个 example.
我的 dash 程序现在很简单,它在没有 nidaqmx 部分的情况下工作(做一些非常简单的事情),而 nidaqmx 程序成功地从传感器读取数据。如果我尝试将它们合并在一起,则会出现以下错误:
这是我的脚本:
import dash
import dash_daq as daq
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input, Output
import plotly.graph_objects as go
from dash.exceptions import PreventUpdate
import nidaqmx
from nidaqmx.constants import AcquisitionType
import numpy as np
external_stylesheets = ['https://codepen.io/chriddyp/pen/bWLwgP.css']
app = dash.Dash(__name__, external_stylesheets=external_stylesheets)
# some color to this
colors = {
'background': '#111111',
'text': '#7FDBFF'
}
#Initial conditions to have something in the graph
trace_1 = go.Scatter(
x = [],
y = []
)
layout = go.Layout(title = 'Título de gráfica')
fig = go.Figure(data = [trace_1], layout = layout)
app.layout = html.Div(style = {'backgroundColor': colors['background']},
children = [
# H3 es para marcar el título, es el mediano que los demás, H1 es súper grande.
html.H3(
children = 'Ejemplo fácil para chequear botones-gráficas-menus',
style = {
'textAlign': 'center',
'color': colors['text']
}
),
# Div parece que divide éste párrafo abajo de lo anterior
html.Div(
id= 'respuesta',
children= 'Clickea el botón',
style = {
'textAlign': 'center',
'color': colors['text']
}
),
# Se define el botón:
daq.BooleanSwitch(
id = 'Swtc_1',
label = 'Capturar',
on = False
),
html.Div(
[
html.Br(),
html.Br(),
html.Label(
['Elija algo'],
style = {
'font-weight': 'bold',
'text-align': 'center',
'color': colors['text']
}
),
dcc.Dropdown(
id = 'f_muestreo',
options = [
{'label' : '2048', 'value':'2048'},
{'label' : '2560', 'value':'2560'},
{'label' : '3200', 'value':'3200'},
{'label' : '5120', 'value':'5120'},
{'label' : '6400', 'value':'6400'},
{'label' : '10240', 'value':'10240'},
{'label' : '12800', 'value':'12800'},
{'label' : '25600', 'value':'25600'}
],
value = '2048',
multi = False,
disabled = False,
persistence = 'string',
persistence_type = 'session'
),
html.Br(),
dcc.Dropdown(
id = 'muestras',
options = [
{'label' : '2048', 'value':'2048'},
{'label' : '4096', 'value':'4096'},
{'label' : '8192', 'value':'8192'},
{'label' : '16384', 'value':'16384'},
{'label' : '32768', 'value':'32768'},
{'label' : '65536', 'value':'65536'},
{'label' : '131072', 'value':'131072'}
],
value = '2048',
multi = False,
disabled = False,
persistence = 'string',
persistence_type = 'session'
)
],className = 'three columns'
),
# graph
html.Div(
[
dcc.Graph(id = 'plot_id', figure = fig)
],className = 'eight columns'
)
]
)
# interaction
@app.callback(
Output('plot_id', 'figure'),
[Input('Swtc_1', 'on'),
Input('f_muestreo', 'value'),
Input('muestras', 'value')]
)
def update_output(on, value_1, value_2):
if on is True:
sample_rate = float(value_1)
samples_to_acq = float(value_2)
wait_time = samples_to_acq/sample_rate
#Name and channel of my NI 9234
channel_name = 'cDAQBAYO1Mod1/ai0'
#Not using the trig yet
#trig_name = 'cDAQBAYO1Mod1/ai1'
cont_mode = AcquisitionType.CONTINUOUS
units_g = nidaqmx.constants.AccelUnits.G
# Create accelerometer channel and configure sample clock and trigger specs
task.ai_channels.add_ai_accel_chan(channel_name, units = units_g)
task.timing.cfg_samp_clk_timing(sample_rate, sample_mode = cont_mode, samps_per_chan=samples_to_acq)
#task.triggers.start_trigger.cfg_dig_edge_start_trig(trigger_source = trig_name)
# Reading data from sensor and generating time data with numpy
ydata = task.read(number_of_samples_per_channel=samples_to_acq)
xdata = np.linspace(0, wait_time,samples_to_acq)
trace_1 = go.Scatter(
x = list(xdata),
y = list(ydata)
)
layout = go.Layout(title = 'Oscilograma')
fig = go.Figure(data = [trace_1], layout = layout)
return (fig)
else:
raise PreventUpdate
if __name__ == '__main__':
app.run_server(port=3040, debug=True)
我在想也许 dash 不支持 nidaqmx,如果是这种情况,一个可能的解决方案是使用两个脚本:一个用于读取传感器,另一个在 dash 中用于显示信息到用户...但是,出于澄清目的,我试图将所有内容都放在一个脚本中。
到处搜索,但找不到在 dash 中实现的 nidaqmx。非常感谢任何帮助。
谢谢,dm2!就像你说的,我没有声明任务,我也 运行 遇到了一个我需要进一步研究的 ctypes 问题,但现在我将声明我的值作为 int。我发布解决方案是因为我猜世界上到处都是像我这样的新手:
import dash
import dash_daq as daq
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input, Output
import plotly.graph_objects as go
from dash.exceptions import PreventUpdate
import nidaqmx
from nidaqmx.constants import AcquisitionType
import numpy as np
external_stylesheets = ['https://codepen.io/chriddyp/pen/bWLwgP.css']
app = dash.Dash(__name__, external_stylesheets=external_stylesheets)
# some color to this
colors = {
'background': '#111111',
'text': '#7FDBFF'
}
#Initial conditions to have something in the graph
trace_1 = go.Scatter(
x = [],
y = []
)
layout = go.Layout(title = 'Título de gráfica')
fig = go.Figure(data = [trace_1], layout = layout)
app.layout = html.Div(style = {'backgroundColor': colors['background']},
children = [
# H3 es para marcar el título, es el mediano que los demás, H1 es súper grande.
html.H3(
children = 'Ejemplo fácil para chequear botones-gráficas-menus',
style = {
'textAlign': 'center',
'color': colors['text']
}
),
# Div parece que divide éste párrafo abajo de lo anterior
html.Div(
id= 'respuesta',
children= 'Clickea el botón',
style = {
'textAlign': 'center',
'color': colors['text']
}
),
# Se define el botón:
daq.BooleanSwitch(
id = 'Swtc_1',
label = 'Capturar',
on = False
),
html.Div(
[
html.Br(),
html.Br(),
html.Label(
['Elija algo'],
style = {
'font-weight': 'bold',
'text-align': 'center',
'color': colors['text']
}
),
dcc.Dropdown(
id = 'f_muestreo',
options = [
{'label' : '2048', 'value':'2048'},
{'label' : '2560', 'value':'2560'},
{'label' : '3200', 'value':'3200'},
{'label' : '5120', 'value':'5120'},
{'label' : '6400', 'value':'6400'},
{'label' : '10240', 'value':'10240'},
{'label' : '12800', 'value':'12800'},
{'label' : '25600', 'value':'25600'}
],
value = '2048',
multi = False,
disabled = False,
persistence = 'string',
persistence_type = 'session'
),
html.Br(),
dcc.Dropdown(
id = 'muestras',
options = [
{'label' : '2048', 'value':'2048'},
{'label' : '4096', 'value':'4096'},
{'label' : '8192', 'value':'8192'},
{'label' : '16384', 'value':'16384'},
{'label' : '32768', 'value':'32768'},
{'label' : '65536', 'value':'65536'},
{'label' : '131072', 'value':'131072'}
],
value = '2048',
multi = False,
disabled = False,
persistence = 'string',
persistence_type = 'session'
)
],className = 'three columns'
),
# graph
html.Div(
[
dcc.Graph(id = 'plot_id', figure = fig)
],className = 'eight columns'
)
]
)
# interaction
@app.callback(
Output('plot_id', 'figure'),
[Input('Swtc_1', 'on'),
Input('f_muestreo', 'value'),
Input('muestras', 'value')]
)
def update_output(on, value_1, value_2):
if on is True:
sample_rate = int(value_1)
samples_to_acq = int(value_2)
wait_time = samples_to_acq/sample_rate
#Name and channel of my NI 9234
channel_name = 'cDAQBAYO1Mod1/ai0'
#Not using the trig yet
#trig_name = 'cDAQBAYO1Mod1/ai1'
cont_mode = AcquisitionType.CONTINUOUS
units_g = nidaqmx.constants.AccelUnits.G
with nidaqmx.Task() as task:
# Create accelerometer channel and configure sample clock and trigger specs
task.ai_channels.add_ai_accel_chan(channel_name, units = units_g)
task.timing.cfg_samp_clk_timing(sample_rate, sample_mode = cont_mode, samps_per_chan=samples_to_acq)
#task.triggers.start_trigger.cfg_dig_edge_start_trig(trigger_source = trig_name)
# Reading data from sensor and generating time data with numpy
ydata = task.read(number_of_samples_per_channel=samples_to_acq)
xdata = np.linspace(0, wait_time,samples_to_acq)
trace_1 = go.Scatter(
x = list(xdata),
y = list(ydata)
)
layout = go.Layout(title = 'Oscilograma')
fig = go.Figure(data = [trace_1], layout = layout)
return (fig)
else:
raise PreventUpdate
if __name__ == '__main__':
app.run_server(port=3040, debug=True)
传感器正在读取数据!!