GithubHelp home page GithubHelp logo

cytoscape / cytosnap Goto Github PK

View Code? Open in Web Editor NEW
56.0 12.0 11.0 132 KB

A Node.js package that renders images of Cytoscape.js graphs on the server using Puppeteer

License: MIT License

JavaScript 96.20% HTML 3.80%
cytoscapejs

cytosnap's Introduction

Cytosnap

Join the chat at https://gitter.im/cytoscape/cytosnap Build Status

Render graphs on the server side with Cytoscape.js, getting image files as output. This package uses Puppeteer to generate the output. Refer to the Puppeteer documentation to ensure that your machine is configured properly to run Chrome headlessly.

This project was initiated at MozSprint 2016

How to contribute

Please refer to CONTRIBUTING.md.

Usage

Quick start example:

var cytosnap = require('cytosnap');

// list of layout extensions to use
// NB you must `npm install` these yourself for your project
cytosnap.use([ 'cytoscape-dagre', 'cytoscape-cose-bilkent' ]);

var snap = cytosnap();

snap.start().then(function(){
  return snap.shot({
    elements: [ // http://js.cytoscape.org/#notation/elements-json
      { data: { id: 'foo' } },
      { data: { id: 'bar' } },
      { data: { source: 'foo', target: 'bar' } }
    ],
    layout: { // http://js.cytoscape.org/#init-opts/layout
      name: 'grid' // you may reference a `cytoscape.use()`d extension name here
    },
    style: [ // http://js.cytoscape.org/#style
      {
        selector: 'node',
        style: {
          'background-color': 'red'
        }
      },
      {
        selector: 'edge',
        style: {
          'line-color': 'red'
        }
      }
    ],
    resolvesTo: 'base64uri',
    format: 'png',
    width: 640,
    height: 480,
    background: 'transparent'
  });
}).then(function( img ){
  // do whatever you want with img
  console.log( img );
});

cytosnap.use()

Pull in layout extensions that you may used in the exported images:

cytosnap.use([ 'cytoscape-dagre', 'cytoscape-cose-bilkent' ]);

Each string is an npm package name that can be pulled in by require(). The list of extension package names that you specify is static: You may specify the list only once, so make sure the list includes all layouts you want to run. Every snap object shares the same extension list.

cytosnap()

Initialise an instance of Cytosnap:

var options = {};

var snap = cytosnap(options);

// or

var snap = new cytosnap(options);

The options you can pass include:

snap.start( [next] )

Start up the Cytosnap instance, snap, so we can request that it generate images:

Promise style:

snap.start().then(function(){ // promise resolved on start
  console.log('chained start promise');
});

Node callback style using next:

snap.start(function( err ){
  console.log('called on start');
});

snap.shot( options, [next] )

Generate a snapshot of a graph:

var defaultOptions = {
  // cytoscape.js options
  elements: undefined, // cytoscape.js elements json
  style: undefined, // a cytoscape.js stylesheet in json format (or a function that returns it)
  layout: undefined // a cytoscape.js layout options object (or a function that returns it)
  // (specifying style or layout via a function is useful in cases where you can't send properly serialisable json)

  // image export options
  resolvesTo: 'base64uri', // output, one of 'base64uri' (default), 'base64', 'stream', or 'json' (export resultant node positions from layout)
  format: 'png', // 'png' or 'jpg'/'jpeg' (n/a if resolvesTo: 'json')
  quality: 85, // quality of image if exporting jpg format, 0 (low) to 100 (high)
  background: 'transparent', // a css colour for the background (transparent by default)
  width: 200, // the width of the image in pixels
  height: 200 // the height of the image in pixels
};

// promise style
snap.shot( defaultOptions ).then(function( img ){
  console.log('on resolve');
}).catch(function( err ){
  console.log('on error');
});

// node callback style
snap.shot( defaultOptions, function( err, img ){
  console.log('on error or resolve');
} );

snap.stop( [next] )

Stop the Cytosnap instance:

Promise style:

snap.stop().then(function(){ // promise resolved on stop
  console.log('chained stop promise');
});

Node callback style using next:

snap.stop(function( err ){
  console.log('called on stop');
});

Targets

  • npm test : Run Mocha tests in ./test

cytosnap's People

Contributors

cmdcolin avatar d2fong avatar maxkfranz avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

cytosnap's Issues

Should not modify phantom instance until after onLoadFinished

i.e.

Shouldn't execute code in Phantom via page.evaluate() or page.evaluateJavaScript() until after onLoadFinished

phantomInstance.on('onLoadFinished', function(){
  // now we can execute
});

Otherwise, we could have a race condition where the Cytoscape instance isn't available yet, since it's loaded on DOMContentLoaded.

cytosnap issue with vite.js

Hi,

I want to use cytosnap in my vite.js project.
But when i import the package, i have this error: "Uncaught (in promise) ReferenceError: require is not defined"

var cytosnap = require('cytosnap'); var snap = cytosnap();

Is there any issue?

Thanks!

Fatal Windows exception code 0xc0000005 PhantomJS has crashed

i tried to drawing data 93,240 nodes, 50,000 edges. but i met this

error: Fatal Windows exception, code 0xc0000005.
PhantomJS has crashed. Please read the bug reporting guide at
http://phantomjs.org/bug-reporting.html and file a bug report.

i think this is memory issues. i am trying is it possible to drawing graph with a lot of data fastly.

when i try using cytoscapejs in only client, i can get graph with little long time. but in cytosnap is fail.

is there any segestion?

Cytosnap.use(spread) not working

I installed the spread extension and tried to use it in cytosnap. It shows 'cytoscape' is not a function. I'm using cytosnap in NodeJs using Puppeteer.

Update Cytoscape dependency

Although the package.json file allows most >=3.1 versions to be used, npm installs the specified version by default. It would be good to upgrade to 3.2.

Can't find variable: cy

Hi, I'm trying to runt the quick start example bug I hit this error:

info: ReferenceError: Can't find variable: cy
info: 
info:   undefined:7 in anonymous
info:   :17

Did I do anything wrong here?

Thanks

Get image export from Phantom process

  • Image could be exported using Phantom image export facilities or Cytoscape image export (Cytoscape may be easier)
  • Image must be passed back to the parent Node process (easier using Phantom mechanism; would have to be via text for Cytoscape base64 image)
  • May want to support export as base64 and export as Node.js File

API definition

Proposal:

  • snap = [new] cytosnap( options ) : get a new instance (new optional)
    • options
    • autostart : whether to automatically start on init (default true)
  • snap.stop() : stops the instance
  • snap.start() : manually starts the instance
  • snap.shot( options, next ) generate a screenshot (returns promise or can use node style callback)
    • options
    • format : 'png' | 'jpg'
    • bg : the background colour of the image (transparent by default).
    • full : whether to export the current viewport view (false, default) or the entire graph (true)
    • scale : This value specifies a positive number that scales the size of the resultant image.
    • maxWidth Specifies the scale automatically in combination with maxHeight such that the resultant image is no wider than maxWidth.
    • maxHeight Specifies the scale automatically in combination with maxWidth such that the resultant image is no taller than maxHeight.

See also mozillascience/global-sprint-2016#25

Executable path option for Puppeteer

return puppeteer.launch({ headless: true, args: snap.options.args });

Hi, we are using Cytosnap to create a dependency graph in our ChatBot, which we run in an Alpine Docker container. I am having issues with launching Chromium in the container. According to Puppeteer troubleshooting docs, the solution is there, but we just need to pass the "executablePath" parameter to Puppeteer.

Would it be possible to add that as an option? If you would like, I can do it as well.

I am using Node 10.15.0 and Cytosnap 3.0.0.

How to use in a thread-safe manner?

From the quick start, we can see that the start function is called then a shot is taken.
If invoked this way, isn't there a performance penalty?

Otherwise I'm considering calling start and reuse the object returned. In this case, can we then call shot multiple times in a thread safe manner?

How to get it running on Vercel ?

Hi there I love this library and everything works when I use it locally.
However once I try to upload my code to production on my vercel site I get a

"chromium revision is not downloaded" error.

Any experience with getting this to run on vercel?

My project uses nuxt 3 so I need node.js 18 at least.

Thnx!

Stylesheet in function format

I'm trying to render an SBGN map on the server side, but I have a problem with the style option. When I provide a stylesheet to cytosnap in JSON format as follows:

elements: cy.json().elements,
style: [
    {
      selector: 'node',
      style: {
        'background-color': 'red'
      }
    }
 ]

This works fine and applies the given style. However, if I provide a stylesheet in function format, it doesn't apply stylesheet and returns an image with default cytoscape.js style. Since I'm trying to render an SBGN map, I used the stylesheet from cytoscape-sbgn-stylesheet as follows:

const cytoscape = require('cytoscape');
const sbgnStylesheet = require('cytoscape-sbgn-stylesheet');
const convertSBGNtoCytoscape = require('sbgnml-to-cytoscape');

let cyData = convertSBGNtoCytoscape(sbgnData);
cy.add(data);
....

snap.start().then(function(){
  return snap.shot({
    elements: cy.json().elements,
    style: sbgnStylesheet(cytoscape),
   ....

but this didn't work. There are two explanations in README about style option:

  • style: undefined, // a cytoscape.js stylesheet in json format (or a function that returns it)
  • // (specifying style or layout via a function is useful in cases where you can't send properly serialisable json)
    From these explanations, I supposed it also accepts stylesheets in function format.

Am I missing some point here or doesn't cytosnap support function format? If it doesn't support, I may try to add it because I think there is no other way to apply sbgn-stylesheet.

Thanks!

Allow options to be specified as non-JSON data

e.g.

snap.shot(function(){
  return {
    elements: [ /* ... */ ],
    style: [
      {
        selector: 'node',
        style: {
          'background-color': function( node ){ return Math.random() > 0.5 ? 'red' : 'blue'; }
        }
      }
    ]
  };
});

Function would get executed in phantom context, so there can't be references outside this scope.

Some node labels appear blurry (low quality)

Hi there,

I am having issues that certain labels only have lower rendered quality than other labels.
The selection of labels seem random...

here's an example: https://yvxeccunoeyjlmbhtexv.supabase.co/storage/v1/object/public/graph_snapshots/graph.jpg

this is my cytosnap code:

const renderSnapshotImage = async (graphData) => {
    const snap = cytosnap()
    await snap.start()

    const img = await snap.shot({
        elements: graphData.elements,
        layout: graphData.layout,
        style: graphData.style,
        resolvesTo: 'base64',
        format: 'png',
        width: 2000,
        height: 2000,
        background: 'rgb(255,248,240)'
    })

    const buff = Buffer.from(img, 'base64')
    return buff
}

`cose` layout doesn't work

The graph does not render properly when using the cose layout:

cose

This image was generated by the test runner, the nodes are all grouped at 0x0.

Any tips on getting this to work?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.