Skip to content

IDEO-coLAB/pyfi-client

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

21 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published