Nodejs library without nodejs - javascript

How can I integrate a nodejs library into my non nodejs project?
I am particularly needing this library:
https://github.com/greenify/biojs-io-blast

BioJS uses Browserify CDN to automatically generate a single JS file for usage. Either include
<script src="http://wzrd.in/bundle/biojs-io-blast#latest"></script>
in your html or download the JS file via this link.
We also have a live JS Bin example here.

Yes, you can do it using a Publisher/Subscribe pattern and a Queue library, such as RabbitMQ.
In the example below, the author is communicating a python script with a NodeJS one, using the RabbitMQ clients for each platform.
https://github.com/osharim/Communicate-Python-with-NodeJS-through-RabbitMQ
The code for sending from NodeJS:
var amqp = require('amqp');
var amqp_hacks = require('./amqp-hacks');
var connection = amqp.createConnection({ host: "localhost", port: 5672 });
connection.on('ready', function(){
connection.publish('task_queue', 'Hello World!');
console.log(" [x] Sent from nodeJS 'Hello World!'");
amqp_hacks.safeEndConnection(connection);
});
Then, receiving in python:
#!/usr/bin/env python
import pika
import time
connection = pika.BlockingConnection(pika.ConnectionParameters(host='localhost'))
channel = connection.channel()
channel.queue_declare(queue='task_queue', durable=True)
#our callback
def suscriber(ch,method , properties , body):
print "[Y] received %r " % (body,)
time.sleep( body.count('.') )
print " [x] Done"
ch.basic_ack(delivery_tag = method.delivery_tag)
channel.basic_qos(prefetch_count=1)
channel.basic_consume(suscriber, queue = 'task_queue')
print ' [*] Waiting for messages from Python. To exit press CTRL+C'
channel.start_consuming()

to integrate any node library you use the package manager NPM https://www.npmjs.com/ so to integrate your library do as follow
open terminal
cd path/to/your/project_dir
type this line
npm install biojs-io-blast

This is the more common use case. Some of the node.js libraby, i like them too much i want to use it everywhere. But this library, what i see uses core modules of node.js like fs. I dont think you can use it without node dependency || node binary. But as Code Uniquely or others folks says, if you are using webpack as a build/dev. You can try, browserify or BioJS

The node_module which provided is kind of xml parser. You can't add nodejs library (node_module) to non nodejs programs. You can get xml parser for Blast depending on kind of programming language you are using.
For example :
For PHP phpBlastXmlParser and
For java this might helpfull

Related

PYTHON module "requests" not working with packaged electron app

In my electron app I have a python file that has two import statements shown below
import requests
import shutil
The app works fine without an errors in the IDE but after packaging the electron app with
npx electron-packager ./ --platform=darwin --icon=src/img/logo.icns
The app gives me this error
It only gives this error for the 'requests' module but not the 'shutil' module. And yes, 'requests' is installed on my computer. I also use the PythonShell module in the js file to run the python file like below
function version_checker(){
form.classList.toggle('hide');
circle.classList.toggle('show');
let pyshell = new PythonShell(__dirname + '/version_checker.py');
pyshell.on('message', function (message){
console.log(message);
if(message !== "same"){
console.log("updating")
}
else{
if(message !== "restart"){
form.classList.toggle('hide');
circle.classList.toggle('show');
}
else{}
}
})
pyshell.end(function (err,code,signal) {
if (err) throw err;
console.log('The exit code was: ' + code);
console.log('The exit signal was: ' + signal);
console.log('finished');
});
}
I also do not wish to compile the python file into an executable because the python script "talks" to the js file. I am also on a M1 Pro Macbook running macOS Ventrua if that helps
How do I package the app and include the 'requests' module OR How do I make the 'requests' module work with the packaged app
I did find a stackoverflow post that asked a similar question but the owner voluntary took down the question so I wasn't able to view it
I looked at this post but it wasn't helpful because I'm not using 'anaconda' or 'child-process'
PythonShell just calls the system's Python interpreter, so use PIP to install the requests library
Or specify the interpreter as follows:
new PythonShell(__dirname + '/version_checker.py', {
pythonPath: '/path/to/python'
})
Note: For production purposes, it is recommended to include an interpreter in electron and install dependencies
It seems you just want to make a HTTP request, why not try fetch in javascript?
If the error is ModuleNotFoundError, then your python interpreter is struggling to find the requests module. This can happen for a number of reasons:
Module does not exist: We can rule this out if your ide ran program successfully.
2: Module is not where interpreter expects to find it: Most likely.
Almost certainly the python interpreter used by your IDE is different to the one you ran after packaging the electron app.
The solution is to either:
a. make sure the location of requests is visible to the PYTHONPATH by setting that in the proper location (I don't use MAC but I believe you have ~/.bash_profile file to set it in or
b. Explicitly add the request module location via a sys.path.append command in your code eg
import sys
import os
path = 'your path to request module'
sys.path.append(os.path.join(path, 'requests')
import requests
You should be able to find the path location by investigating your IDE and looking where you set the module to be included in your program

Using GRPC-Web without NodeJS

How do you use GRPC-Web on the browser?
I mean, in pure browser code, without any NodeJS involved.
Official example from here: https://github.com/grpc/grpc-web/tree/master/net/grpc/gateway/examples/helloworld are mainly NodeJS oriented.
Is there way to use GRPC-Web in pure Javascript form without:
const {HelloRequest, HelloReply} = require('./helloworld_pb.js');
const {GreeterClient} = require('./helloworld_grpc_web_pb.js');
Meaning, just standard <script>-way of adding Javascript dependencies?
And be able to do: var client = new GreeterClient('http://localhost:8080');
Use grpc-web, not #grpc/grpc-js npm package.
The example provided in Write Client Code uses #grpc/grpc-js, which only works on NodeJS.
To use your protobufs and gRPC services defined in your .proto files, you need to generate your code using grpc-web. Then, import those generated files and use them.
Some things I learnt along the way:
You can't really use protobufjs with gprc-web in browsers, you need to use grpc-web and google-protobuf. If someone has an alternative solution, let me know please.
The generated code in grpc-web is not very nice - it is difficult to read and doesn't have all the features of protobufs. You need to have a lot of Javascript and bundling experience to use it.
grpc-web is more limited than gRPC. NodeJS runs gRPC, but in the browser, you have to use grpc-web, and must run a gRPC proxy to translate gRPC-web into/from gRPC.
Yes. You need to bundle your sources with webpack. This step is also described in the documentation you mentioned. At the bottom of the readme:
Just config your webpack to expose variable:
client.js
...
export function instantiateGreeterClient(...) {
return new GreeterClient(...)
};
webpack.config.js
module.exports = {
...
entry: './path/to/client.js',
output: {
path: './bundle/js/',
filename: 'grpc.js',
library: {
name: 'grpc',
type: 'umd',
},
...
}
And after that import your bundle as usual. Now you be able to use all defined variables in your script tag code as
<script src="path/to/grpc.js"></script>
<script>
const client = grpc.instantiateGreeterClient(...)
...
</script>
More information can be found in webpack documentation

I want to call Python in Node.js and use SymPy module

I use Node.js and Express as server side language.
And I made a website using AWS.
I need to use Python because I need complicated calculations.
However, I don't know how to call Python from Node.js.
Please tell me how to use Python's SymPy module in Node.js.
In case you need to call a external script from your Node.js application, you can always spawn a process to do so.
There is (obviously) a npm module to make it easier for you to use a python script from Node.js. You can use python-shell module to launch your "complicated calculation" scrip if you're not able to find the equivalent in Node.js ecosystem.
Here a basic example inspired by the SymPy & python-shell documentation:
script.py
from sympy import *
x = Symbol('x')
print (limit(sin(x)/x, x, 0))
app.js
const {PythonShell} = require('python-shell');
PythonShell.run('script.py', null, function (err, res) {
if (err) throw err;
console.log(res[0]); // 1
});
Also pay attention to have the SymPy module in your path.

Node.js - Call a system command or external command

I've an issue with Node.js. With Python, if I wanted to execute an external command, I used to do something like this:
import subprocess
subprocess.call("bower init", shell=True)
I've read about child_process.exec and spawn in Node.js but I can't do what I want. And what's I want?
I want to execute an external command (like bower init) and see its output in real time and interact with bower itself. The only thing I can do is receive the final output but that don't allow me to interact with the program.
Regards
Edit: I saw this question but the answer doesn't work here. I want to send input when the external program needs it.
How about this?
var childProcess = require('child_process');
var child = childProcess.spawn('bower', ['init'], {
env: process.env,
stdio: 'inherit'
});
child.on('close', function(code) {
process.exit(code);
});
Seemed to work for me

Problems concerning require() javascript

I'm trying to run the JavaScript client for the Tumblr API but I'm getting a "require not defined" error. I've scoured almost every corner of SO trying to find a way to make it work.
I've managed to use Browserify to recognize it but at some point, I'm getting a fs.readFileSync error.
Then I tried RequireJS and again it does the require thing but I'm getting a Module name "something" has not been loaded yet for context"
I'm really new to Node.js but if I'm understanding it correctly the require() part isn't something the browser can do right? Can anyone guide me as to what solutions I can take for this? If it helps, I'm not planning to upload this on a server or anything. I'm just trying to run it on my local computer and just planning to run the API locally.
Edit:
Here's what I have on my main.js
var tumblr = require('index.js');
var client = tumblr.createClient({
consumer_key: '<consumer key>',
consumer_secret: '<consumer secret>',
token: '<token>',
token_secret: '<token secret>'
});
// Show user's blog names
client.userInfo(function (err, data) {
data.user.blogs.forEach(function (blog) {
alert('dang');
});
});
When I try requirejs, I do this to reference it <script data-main="script/main" src="scripts/require.js"></script>
It seems to me that you might be confusing server-side JavaScript (Node) and client-side JavaScript (in the browser). The Tumblr API you're trying to use is for Node only; you can't run it in a browser. You have to:
Install NodeJS and NPM on your system
npm install tumblr.js
Run the script with node <script name>
Start with the Tumblr.js example script, then customize for your own purposes!

Categories

Resources