从Python的破折号库转换为Node.js

时间:2019-10-14 23:30:59

标签: python node.js express

我正在尝试将Python破折号中完成的代码转换为Node.js + express。我已经构建了UI,但是现在我需要合并用Python编写的用于更新UI的功能。

例如Python中的此函数:

    # The Map generator    
@app.callback(Output('plot1', 'figure'),[Input('button', 'n_clicks')],
    [State('text1', 'value'),State('text2', 'value'),State('text3', 'value'),State('text4', 'value')])


def text_callback(building_address, year_end='2017',year_start='2016', mapping_variable = 'Median Household Income'):
    print("Using this function now")

    addresses = building_address.split(';')
    print("ADDRESSESsssssssssssssssssssss:", addresses)
    data_slider = []
    layers_slider = []
    if mapping_variable == 'Predicted Growth Rank':
        colormap = 'Blues_r'
    else:
        colormap = 'Blues'
    gdf =  get_gdf(addresses, all_data, year_end,year_start)[0]
    Building_lat,Building_lon,Building_name = get_building_info(addresses)
    cmin=gdf[mapping_variable].min()
    cmax=gdf[mapping_variable].max()
    sm= scalarmappable(colormap, cmin, cmax)
    colorscale_new = get_colorscale(sm, df=gdf[mapping_variable], cmin=gdf[mapping_variable].min(), cmax=gdf[mapping_variable].max())
    pl_colorscale = colorscale_new
    gdf_year =  gdf[gdf['Year']==year_end]
    gdf_year['Predicted Growth Rank'] = gdf_year['Predicted Growth Rank'].rank(ascending=False)
    gdf_year= gdf_year.reset_index()
    index_list = list(gdf_year.index)
    centers,layers = get_mapbox_choropleth_data(gdf_year, index_list, col_vals=mapping_variable, colorscale=colorscale_new, val_name=mapping_variable)

    data_slider.append(centers)
    layers_slider.append(layers)    
    data2 = go.Scattermapbox(
        lat=Building_lat, lon=Building_lon, mode='markers', marker=go.scattermapbox.Marker(size=9,color='rgb(231, 41, 74)'),
        showlegend=False,
        text=Building_name) 
    data_slider.append(data2)
    layoutmbx = dict(font=dict(family='Balto', size=16), autosize=False, height=740, width=1200,
                     hovermode='closest',
        mapbox=dict(accesstoken=mapbox_access_token,layers=layers,
                bearing=0,
                center=dict(lat=centers['lat'][0], lon=centers['lon'][0]),
                pitch=0,
                zoom=9,style = 'streets'),margin = dict(l = 0, r = 0, t = 0, b = 0),
                     plot_bgcolor= '#202d56',
                paper_bgcolor='#202d56')    
    return {'data': data_slider, 'layout': layoutmbx}

@app.callback映射前端的不同元素,并按照函数需要使用它们的顺序对其进行标识。

就像..按钮= n_clicks,text1 =建筑物地址,text2 =年末,text3 = year_start,text4 =映射变量。

如何将Python的Dash转换为Node.JS + Express?

更新:

更具体地说,我现在拥有的UI具有4个变量:address,year_end,year_start和mapping_variable。上面针对一个特定地址的函数将返回以下内容:

[{'type': 'scattermapbox', 'mode': 'markers', 'text': ['Census Tract:304.01<br>Median Household Income:52212<br> Year: 2017 '], 'marker': {'size': 1, 'color': ['rgba(247, 251, 255, 255)'], 'colorscale': [[0.0, 'rgba(247, 251, 255, 255)'], [0.05263157894736842, 'rgba(236, 244, 251, 255)'], [0.10526315789473684, 'rgba(226, 237, 248, 255)'], [0.15789473684210525, 'rgba(215, 230, 244, 255)'], [0.21052631578947367, 'rgba(206, 224, 241, 255)'], [0.2631578947368421, 'rgba(193, 217, 237, 255)'], [0.3157894736842105, 'rgba(177, 210, 231, 255)'], [0.3684210526315789, 'rgba(160, 202, 225, 255)'], [0.42105263157894735, 'rgba(139, 192, 221, 255)'], [0.47368421052631576, 'rgba(117, 179, 216, 255)'], [0.5263157894736842, 'rgba(98, 168, 210, 255)'], [0.5789473684210527, 'rgba(80, 155, 203, 255)'], [0.631578947368421, 'rgba(64, 144, 197, 255)'], [0.6842105263157894, 'rgba(49, 129, 189, 255)'], [0.7368421052631579, 'rgba(36, 116, 182, 255)'], [0.7894736842105263, 'rgba(24, 102, 172, 255)'], [0.8421052631578947, 'rgba(14, 89, 162, 255)'], [0.894736842105263, 'rgba(8, 74, 146, 255)'], [0.9473684210526315, 'rgba(8, 61, 126, 255)'], [1.0, 'rgba(8, 48, 107, 255)']], 'opacity': 0, 'cmin': 52212, 'cmax': 52212, 'showscale': False}, 'showlegend': False, 'lon': [-122.77720976814757], 'lat': [45.47996783203806], 'hoverinfo': 'text'}, Scattermapbox({
    'lat': [45.48553],
    'lon': [-122.77518],
    'marker': {'color': 'rgb(231, 41, 74)', 'size': 9},
    'mode': 'markers',
    'showlegend': False,
    'text': [9555 SW DUNCAN LN]
})]

此函数在dash API中在python中的作用是将这些颜色叠加到地图上。我必须对Node.JS做同样的事情。

2 个答案:

答案 0 :(得分:1)

TLDR;

而不是将py转换为js 也许您可以调用子进程来调用py?

参考:https://www.geeksforgeeks.org/run-python-script-node-js-using-child-process-spawn-method/

答案 1 :(得分:1)

有多种从JS调用python的方法。它不是推荐的方法,也不是首选花费时间进行转换。请根据我以前的回答在下面查看我的建议。

  • 第一种方法是进行npm install python-shell

这是代码

var PythonShell = require('python-shell');
//you can use error handling to see if there are any errors
PythonShell.run('my_script.py', options, function (err, results) { 
//your code

您可以使用以下命令向python shell发送消息 pyshell.send('hello');

您可以在此处找到API参考- https://github.com/extrabacon/python-shell

  • 第二种方式-您可以引用的另一个包是node python,您必须执行npm install node-python

  • 第三种方式-您可以找到使用子进程的示例- 使用另一个答案below。 其他一些参考- https://www.npmjs.com/package/python

如果要使用面向服务的体系结构- http://ianhinsdale.com/code/2013/12/08/communicating-between-nodejs-and-python/

Here是我以前的答案的参考