Search code examples
handlebars.jscytoscape.js

Using express-handlebars with Cytoscape.js


Hi I'm new to web development and I'm trying to play around with Cytoscape.js. I decided to use Node.js, Express, and Express-handlebars to run my webpage. I wanted to use Cytoscape.js to display a graph however I'm not sure how to use handlebars to get a reference to the container to initialize the cytoscape object. Here's my main.handlebars file:

<!doctype html>
<html>
<head>
    <title>Hello Cytoscape</title>
</head>
<body>
   {{{body}}}
</body>
</html>

Here's my home.handlebars file:

<div id="cy"></div>  

Here's my .js file:

/**
 * 
 */
'use strict';

var express = require('express');
var app = express();

var cytoscape = require('cytoscape');

//layout defaults to main, located in the views layout folder
var handlebars = require('express-handlebars').create({defaultLayout:'main'});

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

//sets the template engine to use for the express object
//a template engine will implement the view part of the app
app.engine('handlebars', handlebars.engine);
app.set('view engine', 'handlebars');


//initialize the cytoscape object
var cy = cytoscape({

  //<----not sure how to do this----->
  container: document.getElementById('cy'), // container to render in

  elements: [ // list of graph elements to start with
          { // node a
                  data: { id: 'a' }
          },
          { // node b
                  data: { id: 'b' }
          },
          { // edge ab
                  data: { id: 'ab', source: 'a', target: 'b' }
          }
  ],

  style: [ // the stylesheet for the graph
          {
                  selector: 'node',
                  style: {
                          'background-color': '#666',
                          'label': 'data(id)'
                  }
          },

          {
          selector: 'edge',
                  style: {
                  'width': 3,
                  'line-color': '#ccc',
                  'target-arrow-color': '#ccc',
                  'target-arrow-shape': 'triangle'
                  }
          }
  ],

  layout: {
          name: 'grid',
          rows: 1
  },
  // initial viewport state:
  zoom: 1,
  pan: { x: 0, y: 0 },

  // interaction options:
  minZoom: 1e-50,
  maxZoom: 1e50,
  zoomingEnabled: true,
  userZoomingEnabled: true,
  panningEnabled: true,
  userPanningEnabled: true,
  boxSelectionEnabled: false,
  selectionType: 'single',
  touchTapThreshold: 8,
  desktopTapThreshold: 4,
  autolock: false,
  autoungrabify: false,
  autounselectify: false,
  // rendering options:
  headless: false,
  styleEnabled: true,
  hideEdgesOnViewport: false,
  hideLabelsOnViewport: false,
  textureOnViewport: false,
  motionBlur: false,
  motionBlurOpacity: 0.2,
  wheelSensitivity: 1,
  pixelRatio: 'auto'
});                    

app.get('/', function (req, res) {
   var context = {};    
   res.render('home', context); 
});

//listener all for unrecognized urls
//return 404 not found response
app.use(function(req,res){
res.status(404);
res.render('404');
});

//listener for errors generate on server
//return 500 response
app.use(function(err, req, res, next){
console.error(err.stack);
res.type('plain/text');
res.status(500);
res.render('500');
});


if (module === require.main) {
// [START server]
// Start the server
var server = app.listen(process.env.PORT || 8080, function () {
 var port = server.address().port;
 console.log('App listening on port %s', port);
});
// [END server]
}

module.exports = app;

So I guess my question is how do I get a reference to the div container id="cy" to initialize my Cytoscape.js graph using express-handlebars, thanks in advance for any help.


Solution

  • If you run Cytoscape on the serverside -- as you're doing in your example -- then it's not running and not shown on the clientside.

    Unless you're doing serverside-only graph analysis, you should be using Cytoscape on the clientside. Your page (main.handlebars) is the driver of everything clientside, so put your Cytoscape code there. Or in the page, reference your Cytoscape code with a <script> tag.