剧情:带有实时图形的破折号按钮不起作用

时间:2018-08-31 06:59:15

标签: python plotly plotly-dash

我开发了基于python破折号的应用程序进行监视。作为项目的一部分,我想显示实时图形并根据用户输入更改实时图形值。我被困在这一部分。当我开始在输入框(例如:temp)中输入内容时,实时图将得到更新,该图将继续更新每个字母,例如t,te,tem,temp。所以我创建了一个按钮来提交输入值。还是每个字母的图形都会更新。

相同的代码:

app = dash.Dash(__name__)
app.layout = html.Div(
    [ 
    dcc.Input(id='input-value', value='example', type='text'),
    html.Button('Submit', id="submit-button"),
    dcc.Graph(id='live-graph', animate=False),
    dcc.Interval(
        id='graph-update',
        interval=1*1000
    ),
]
)

回叫功能如下

@app.callback(Output('live-graph', 'figure'),
          [Input('submit-button','n_clicks')],
          state=[State(component_id='sentiment_term', component_property='value')],
          events=[Event('graph-update', 'interval')])

def update_graph_scatter(n_clicks, input_value):
    conn = sqlite3.connect('database.db')
    c = conn.cursor()
    df = pd.read_sql("SELECT * FROM table WHERE colume LIKE ? ORDER BY unix DESC LIMIT 1000", conn ,params=('%' + input_value+ '%',))
    df.sort_values('unix', inplace=True)

    df['date'] = pd.to_datetime(df['unix'],unit='ms')
    df.set_index('date', inplace=True)

    X = df.index
    Y = df.column

    data = plotly.graph_objs.Scatter(
            x=X,
            y=Y,
            name='Scatter',
            mode= 'lines+markers'
           )

    return {'data': [data],'layout' : go.Layout(xaxis=dict(range=   [min(X),max(X)]),
                                            yaxis=dict(range=[min(Y),max(Y)])}

注意:如果我删除了间隔,按钮将开始工作。但是我想实时更新按钮

2 个答案:

答案 0 :(得分:1)

您可以使用诸如div之类的中间组件来保存来自文本字段的输入,并仅在单击按钮时更新该div。 所以你会

function getHashs(string id) public view returns (bytes32[]) {
    bytes32[] memory stringsToHash = getStrings(id);
    bytes32[] memory hashes = new bytes32[](5);

    for(uint i=0; i<=stringsToHash.length-1; i++) {
        bytes32 str = seeds[i];
        bytes32 hash = sha256(abi.encodePacked(seed));
        hashes[i] = hash;
    }

    return hashes;
}

现在,仅在单击按钮时更新div:

w3

并且Graph始终使用div内容查询数据库(间隔触发或div更改时):

function getHashes(id, contract, fromAccount, privateForNode) {

   return new Promise(function(resolve, reject) {

       contract.methods.getHashs(id).send({from: fromAccount, gas: 150000000, privateFor: [privateForNode]})
       .then(hashes => {
           console.log("got hashes from node === ");
           console.log(hashes[0]); // this is 'undefined'
          resolve(hashes);
       },
       (error) => {
           reject(error);
       }).catch((err) => {
            reject(err);
       });

   })
   .catch((err) => {
      reject(err);
   });
}

答案 1 :(得分:0)

您确定每个字母的更新都是由于输入的原因吗?听起来像是间隔使用您输入的未完成输入来更新图形。

P.S .:我想您在输入和州ID之间存在名称差异。