1.0.4 • Published 6 years ago

pyfi-client v1.0.4

Weekly downloads
1
License
MIT
Repository
github
Last release
6 years ago

pyfi-client CircleCI License: MIT npm

Quickly make Python functions available to a javascript client when used along with PyFi

Usage

Pyfi-Client duplicates the functionality exposed to the server and makes it available to the client via Socket.io.

Setting up a server

Getting up and running is pretty simple. From your Node PyFi instance, attach a socket.io instance:

py._.attachClientSocketIO(io)

Here's a full example using express:

const express = require('express');
const PyFi = require('pyfi');

const app = require('express')();
const server = require('http').Server(app);
const io = require('socket.io')(server);

app.use(express.static('public'));


const py = PyFi({
    path: './python',
    imports: [{
      import: ['tell_me_the_time'],
      from: 'timing',
    }],
  });

py._.attachClientSocketIO(io);

// wait for PyFi to init
py._.onReady(() => {
  server.listen(3000, () => {
    console.log('listening on port 3000!');
  });
});

You can find the full working example here.

Connecting a Client

Once you have a server set up, it's quite straightforward to connect a client.

const PyFiClient = require('pyfi-client');
// or
import PyFiClient from 'pyfi-client';

const py = PyFiClient(http://localhost:3000)

py._.onReady(()=>{
  py.tell_me_the_time()
  .then(result => {
    console.log(result)
  })
  .catch(error => {
    // handle error
  })
})

PyFiClient automatically duplicates all of the Python functions imported on the server on the client.

Sending messages mid-execution

Just like in PyFi, PyfiClient supports receiving messages from Python while a function is running. That allows for, for example, streaming status back to a client while a long-running function is in progress.

It looks very similar to PyFi on Node: Python:

def my_function():
  # ... do something ...
  pyfi_message('my message')
  # ... do something else ...
  return 'done!'

Client:

py.my_function()
  .onMessage(data => {
    console.log(data)
    // 'my message'
  })
  .then(res => {
    console.log(res);
    // 'done'
  })

Reference

PyFiClient(address)

Returns a PyFiClient instance and initializes callables that match those available in the server instance of PyFi.

You may provide an address for the serer (i.e. http://localhost:3000), or if no server is provided, the client will connect to the host address. This matches the functionality of socket.io.

Methods

_.onReady(callback) Attach a callback function to call when the instance of PyFiClient is ready.

Contributing

We welcome issues and pull requests.

If you spot a bug, please provide as much context as possible – including your OS, which versions of Node and Python you're running on, how you're managing your Python environment, and anything else that could be relevant.

License

MIT License (c) 2018 - Present IDEO CoLab

1.0.4

6 years ago

1.0.3

6 years ago

1.0.2

6 years ago

1.0.1

6 years ago

1.0.0

6 years ago