gramex.pynode

Python Node.js bridge

Node(port=9966, cwd=None, node_path=None, timeout=10)

Example

node = Node(port=9966, node_path=’/tmp/’) total = await node.js(‘return {“total”: x + y}’, x=’a’, y=10)[‘total’]

Source code in gramex\pynode.py
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
def __init__(
    self, port: int = 9966, cwd: str = None, node_path: str = None, timeout: int = 10
):
    '''
    Example:

        >>> node = Node(port=9966, node_path='/tmp/')
        >>> total = await node.js('return {"total": x + y}', x='a', y=10)['total']
    '''
    self.port = port
    # cwd is the directory where node runs. Defaults to $GRAMEXDATA/pynode/
    # node_modules is updated under this.
    self.cwd = os.path.join(variables['GRAMEXDATA'], 'pynode') if cwd is None else cwd
    if not os.path.exists(self.cwd):
        os.makedirs(self.cwd)
    # node_path is where the node executable is. Autodetect from PATH by default
    self.node_path = node_path or which('node')
    self.url = f'ws://127.0.0.1:{port}'
    self.timeout = timeout
    self.proc, self.conn = None, None
    self.count = 0