Tôi đã cố gắng giao tiếp giữa nút và Python, tôi muốn gửi mảng đối tượng đến Python và in nó bằng Python, nhưng mã của tôi không hoạt động.
content=[
{
"username": "admin",
"first_name": "",
"last_name": "",
"roles": "system_admin system_user",
"locale": "en",
"delete_at": 0,
"update_at": 1511335509393,
"create_at": 1511335500662,
"auth_service": "",
"email": "",
"auth_data": "",
"position": "",
"nickname": "",
"id": "pbjds5wmsp8cxr993nmc6ozodh"
},
{
"username": "chatops",
"first_name": "",
"last_name": "",
"roles": "system_user",
"locale": "en",
"delete_at": 0,
"update_at": 1511335743479,
"create_at": 1511335743393,
"auth_service": "",
"email": "",
"auth_data": "",
"position": "",
"nickname": "",
"id": "akxdddp5p7fjirxq7whhntq1nr"
}]
Mã JavaScript:
const express=require['express']
const app=express[]
let p = require['python-shell'];
app.get['/send',[req,res]=>{
var options = {
args:
[
content
]
}
p.PythonShell.run['hello.py', options, function [err, results] {
console.log[results.toString[]]
}];
}]
app.listen['5000']
Kịch bản Python:
import sys
import json
details=sys.argv[1]
print [details]
Kết nối với cơ sở dữ liệu Cloudant để phân tích [Phần 2]
Trong phần một của loạt bài này, chúng tôi đã thấy làm thế nào chúng ta có thể:
- Sử dụng mã Node.js bên trong sổ ghi chép Jupyter bằng cách thêm pixiedust_node
- Sử dụng
6 vàconst express=require['express'] const app=express[] let p = require['python-shell']; app.get['/send',[req,res]=>{ var options = { args: [ content ] } p.PythonShell.run['hello.py', options, function [err, results] { console.log[results.toString[]] }]; }] app.listen['5000']
7 trong mã JavaScript và Python để xuất và trực quan hóa dữ liệuconst express=require['express'] const app=express[] let p = require['python-shell']; app.get['/send',[req,res]=>{ var options = { args: [ content ] } p.PythonShell.run['hello.py', options, function [err, results] { console.log[results.toString[]] }]; }] app.listen['5000']
- Thêm các mô -đun NPM vào sổ ghi chép của chúng tôi và xây dựng các chức năng bằng cách sử dụng các cuộc gọi lại hoặc lời hứa
Trong blog này, chúng tôi sẽ chia sẻ dữ liệu giữa Node.js và mã Python và sử dụng máy tính xách tay để khám phá dữ liệu trong cơ sở dữ liệu mây.
Biên tập viên Lưu ý: Chia sẻ dữ liệu giữa các tế bào Node.js và Python thậm chí còn dễ dàng hơn. Để biết thêm, xem bài viết năm 2018 này: Sharing data between Node.js and Python cells is now even easier. For more, see this 2018 article:
Chia sẻ dữ liệu giữa các tế bào Node.js & Python
Hãy nói rằng chúng tôi có một số dữ liệu trong ô Node.js, trong trường hợp này là một chuỗi thời gian với một số dữ liệu dạng sóng trong một biến có tên là
const express=require['express']
const app=express[]
let p = require['python-shell'];
app.get['/send',[req,res]=>{
var options = {
args:
[
content
]
}
p.PythonShell.run['hello.py', options, function [err, results] {
console.log[results.toString[]]
}];
}]
app.listen['5000']
8:%%node
var wave = [];
for [var i = 0; i < 1000; i++] {
var x = 2*Math.PI * i/ 360;
var obj = {
x: x,
i: i,
sin: Math.sin[x],
cos: Math.cos[x],
tan: Math.tan[x]
};
wave.push[obj];
}
Chúng tôi đã thấy cách gọi
const express=require['express']
const app=express[]
let p = require['python-shell'];
app.get['/send',[req,res]=>{
var options = {
args:
[
content
]
}
p.PythonShell.run['hello.py', options, function [err, results] {
console.log[results.toString[]]
}];
}]
app.listen['5000']
9 để xuất dữ liệu dưới dạng JSON và import sys
import json
details=sys.argv[1]
print [details]
0 để gửi dữ liệu đến công cụ trực quan của Pixiedust, nhưng có một cách thứ ba: import sys
import json
details=sys.argv[1]
print [details]
1.Hàm JavaScript
import sys
import json
details=sys.argv[1]
print [details]
2 có hai tham số:- Biến JavaScript để sử dụng [
8]const express=require['express'] const app=express[] let p = require['python-shell']; app.get['/send',[req,res]=>{ var options = { args: [ content ] } p.PythonShell.run['hello.py', options, function [err, results] { console.log[results.toString[]] }]; }] app.listen['5000']
- Tên của biến Python bạn muốn gửi đến [
4]import sys import json details=sys.argv[1] print [details]
Nếu chúng ta chạy đoạn trích này trong một ô:
%%node
store[wave, 'w'];
Sau đó, trong tế bào Python tiếp theo, chúng ta có thể truy cập
import sys
import json
details=sys.argv[1]
print [details]
4, đây là một khung dữ liệu gấu trúc đã sẵn sàng để phân tích trong Python. Chúng tôi có thể trộn và kết hợp mã Node.js và Python trong cùng một cuốn sổ, chia sẻ dữ liệu JavaScript với mã Python của bạn!# count of dataframe
print w.count[]
# maximum value of the sin wave
print w['sin'].max[]
# minimum value of the sin wave
print w['sin'].min[]
# average value of the tan wave
print w['tan'].mean[]
Truy cập cơ sở dữ liệu Cloudant từ máy tính xách tay
Để truy cập dữ liệu được lưu trữ trong cơ sở dữ liệu Cloudant, chúng tôi có thể sử dụng mô-đun NPM Cloudant-Quickstart:
npm.install['cloudant-quickstart']
Với URL Cloudant của chúng tôi, chúng tôi có thể bắt đầu khám phá dữ liệu trong Node.js. Trước tiên, chúng tôi tạo kết nối với cơ sở dữ liệu Cloudant từ xa:
%%node
// connect to Cloudant using cloudant-quickstart
var cloudantqs = require['cloudant-quickstart'];
var cities = cloudantqs['//reader.cloudant.com/cities'];
Khám phá dữ liệu bằng Node.js trong sổ ghi chép
Bây giờ chúng ta có một đối tượng
import sys
import json
details=sys.argv[1]
print [details]
6 mà chúng ta có thể sử dụng để truy cập cơ sở dữ liệu. Nếu chúng ta biết ID của các tài liệu, chúng ta có thể truy xuất chúng đơn lẻ:%%node
cities.get['2636749'].then[print];
// {"name": "Stowmarket", "country": "GB", "longitude": 0.99774, "latitude": 52.18893, "timezone": "Europe/London", "_id": "2636749", "population": 15394}
Hoặc với số lượng lớn:
%%node
cities.get[['4562407','2636749','3530597']].then[print];
// [{"name": "York", "country": "US", "longitude": -76.72774, "latitude": 39.9626, "timezone": "America/New_York", "_id": "4562407", "population": 43718}, {"name": "Stowmarket", "country": "GB", "longitude": 0.99774, "latitude": 52.18893, "timezone": "Europe/London", "_id": "2636749", "population": 15394}, {"name": "Mexico City", "country": "MX", "longitude": -99.12766, "latitude": 19.42847, "timezone": "America/Mexico_City", "_id": "3530597", "population": 12294193}]
Thay vì chỉ gọi
const express=require['express']
const app=express[]
let p = require['python-shell'];
app.get['/send',[req,res]=>{
var options = {
args:
[
content
]
}
p.PythonShell.run['hello.py', options, function [err, results] {
console.log[results.toString[]]
}];
}]
app.listen['5000']
6 để xuất JSON, chúng ta có thể mang chức năng const express=require['express']
const app=express[]
let p = require['python-shell'];
app.get['/send',[req,res]=>{
var options = {
args:
[
content
]
}
p.PythonShell.run['hello.py', options, function [err, results] {
console.log[results.toString[]]
}];
}]
app.listen['5000']
7 của PixieDust để truyền cho nó một mảng dữ liệu để trực quan hóa:const express=require['express']
const app=express[]
let p = require['python-shell'];
app.get['/send',[req,res]=>{
var options = {
args:
[
content
]
}
p.PythonShell.run['hello.py', options, function [err, results] {
console.log[results.toString[]]
}];
}]
app.listen['5000']
0 Mô-đun NPM Cloudant-Quickstart truy vấn cơ sở dữ liệu đám mây, từ trong bối cảnh của một cuốn sổ tay Jupyter Python.Chúng tôi cũng có thể truy vấn một tập hợp con của dữ liệu bằng hàm
import sys
import json
details=sys.argv[1]
print [details]
9, chuyển nó một câu lệnh truy vấn đám mây:const express=require['express']
const app=express[]
let p = require['python-shell'];
app.get['/send',[req,res]=>{
var options = {
args:
[
content
]
}
p.PythonShell.run['hello.py', options, function [err, results] {
console.log[results.toString[]]
}];
}]
app.listen['5000']
1Tổng hợp dữ liệu
Thư viện Cloudant-Quickstart cũng cho phép các tập hợp [tổng, đếm, số liệu thống kê] được thực hiện trong cơ sở dữ liệu Cloudant.
Hãy để tính toán tổng của trường
%%node
var wave = [];
for [var i = 0; i < 1000; i++] {
var x = 2*Math.PI * i/ 360;
var obj = {
x: x,
i: i,
sin: Math.sin[x],
cos: Math.cos[x],
tan: Math.tan[x]
};
wave.push[obj];
}
0:const express=require['express']
const app=express[]
let p = require['python-shell'];
app.get['/send',[req,res]=>{
var options = {
args:
[
content
]
}
p.PythonShell.run['hello.py', options, function [err, results] {
console.log[results.toString[]]
}];
}]
app.listen['5000']
2Hoặc tính tổng số dân số, được nhóm theo trường
%%node
var wave = [];
for [var i = 0; i < 1000; i++] {
var x = 2*Math.PI * i/ 360;
var obj = {
x: x,
i: i,
sin: Math.sin[x],
cos: Math.cos[x],
tan: Math.tan[x]
};
wave.push[obj];
}
1:const express=require['express']
const app=express[]
let p = require['python-shell'];
app.get['/send',[req,res]=>{
var options = {
args:
[
content
]
}
p.PythonShell.run['hello.py', options, function [err, results] {
console.log[results.toString[]]
}];
}]
app.listen['5000']
3Chúng ta thậm chí có thể chuyển dữ liệu sang Python bằng hàm
import sys
import json
details=sys.argv[1]
print [details]
2:const express=require['express']
const app=express[]
let p = require['python-shell'];
app.get['/send',[req,res]=>{
var options = {
args:
[
content
]
}
p.PythonShell.run['hello.py', options, function [err, results] {
console.log[results.toString[]]
}];
}]
app.listen['5000']
4Sau đó, chúng ta có thể truy cập
%%node
var wave = [];
for [var i = 0; i < 1000; i++] {
var x = 2*Math.PI * i/ 360;
var obj = {
x: x,
i: i,
sin: Math.sin[x],
cos: Math.cos[x],
tan: Math.tan[x]
};
wave.push[obj];
}
3 trong ô Python:const express=require['express']
const app=express[]
let p = require['python-shell'];
app.get['/send',[req,res]=>{
var options = {
args:
[
content
]
}
p.PythonShell.run['hello.py', options, function [err, results] {
console.log[results.toString[]]
}];
}]
app.listen['5000']
5Cuộc gọi để lưu trữ rất hữu ích cho số lượng dữ liệu hợp lý, nhưng vì tất cả các dữ liệu phải nằm trong bộ nhớ, nó không phù hợp với các bộ dữ liệu rất lớn.
Cái gì tiếp theo?
Trong phần tiếp theo, chúng tôi sẽ tìm cách đưa các mô -đun NPM tiếp theo vào sổ ghi chép của chúng tôi, xây dựng các hình ảnh tùy chỉnh của riêng mình.