MeVisLab Web Development

Concepts

MeVisLab-based applications can implement a client-server scenario where a web browser is the client.

Note

To be able to use this functionality, a license that allows to develop web applications is needed.

MeVisLab provides the following concepts for this:

Web Macro Module Wizard

The Web Macro Module Wizard creates the skeleton of a web macro module. A web macro module is from the MeVisLab perspective a normal macro module. The difference is that the web variant requires some extra MDL tags and Python code. It may also provide a JavaScript plugin, a CSS file, and an HTML file.

figures/WebMacroModuleWizard.png

JavaScript Plugins for the MWT

This section of the wizard lets you configure whether you want to create a custom JavaScript plugin. Such a plugin allows you to run JavaScript code in the web browser after the MeVisLab Web Toolkit has been initialized. A skeleton will be generated for the plugin, which will create a custom namespace and a Plugin class. A name must be provided for this namespace.

The MWT provides some third-party plugins, which you can also include in your web macro module.

HTML and CSS File

You can choose to create an empty CSS file for your project for customizing the style of the web application here. An HTML file that initializes the MWT can also be created. It will also be used in the developer URL of the web application, so that the startWorkerService.py user script loads this instead of a generic HTML file.

Remote Modules

Remote Modules allow MeVisLab to run modules in a separate process. The inter-process communication is implemented through a remote messaging protocol over a TCP connection. A MeVisLab module is created in the so-called worker process, a background process.

The MeVisLab process communicates with that module using a remote module, which has the same field interface as the actual module and synchronizes field changes in both directions. The remote modules support also web socket connections, which allows for using web browsers as clients. A worker process serves each connection from a web browser.

MeVisLab Web Toolkit

The MeVisLab Web Toolkit Reference is a JavaScript framework that provides a web browser implementation of the remote modules (see the MeVisLab Web Toolkit Reference, section Communication). It is based on the HTML5 web socket, which is managed by the class MLAB.Core.RemoteManager.

The field support is almost the same as in the underlying Remote Modules, except that image output fields are not supported, because image processing does not take place in web browsers.

By default, there are Base field handlers for the types RemoteCallInterface, RemoteRendering, and StandardItemModel.

It is possible to add own handlers for custom Base types. They must be derived from MLAB.Core.BaseFieldHandlerBase and must implement the handleMessage() method. See also section Base Field Communication in the Remote Modules overview.

The API provides classes including modules, fields, and controls, which attempt to be similar to the C++ MeVisLab API. The control implementations use widget classes to separate the DOM manipulation code from the controls. Both controls and widgets are registered with a unique name in a factory: MLAB.GUI.WidgetControlFactory and MLAB.GUI.WidgetFactory.

Overwriting already registered classes is possible. This allows plugins to replace existing control and widget classes by their own implementations. Look at the yui plugin for an example. Another feature is the signal/slot mechanism that intends to ease the frequent callback handling in JavaScript.

Classes

JavaScript has no real class concept; instead there are objects and prototypes. The MeVisLab Web Toolkit emulates classes and inheritance by using prototypes. The classes are organized in namespaces. A namespace is a Javascript object that belongs to the global namespace MLAB. Namespaces are created with the function MLAB.createNamespace().

To create a class, use the defineClass() and deriveClass() functions of the namespace in which you want to place the class, for example MLAB.Core.defineClass() and MLAB.Core.deriveClass().

Modules

Modules represent MeVisLab modules that exist on the server side. They own the field objects that represent the fields of the MeVisLab module.

When modules are created in the web browser, they use the remote manager to create and connect to a worker process on the server, in which the remote module is created. Modules are created by MLAB.runApplication(), by explicitly calling MLAB.GUI.Application.createModule(), and by special elements in the DOM. These elements must exist initially when the MeVisLab Web Toolkit is initializing; otherwise, it will not find them. The elements must include the data-mevislab-create-macro attribute. Such a DOM element would look like this:

<div data-mevislab-create-macro="module:'SomeModule', instanceName:'SomeInstanceName'"></div>

The instance name is optional. By default, the module name will be the instance name. If the module provides a panel or window that should be shown in the web page, then elements with the data-mevislab-show-panel and data-mevislab-show-window attributes can be used and placed anywhere in the <body>:

<div data-mevislab-show-panel="module:'SomeInstanceName'"></div>
<div data-mevislab-show-window="module:'SomeInstanceName'"></div>

It is also possible to programmatically show a window or panel. MLAB.GUI.Application.module() provides access the module. The module itself has a showWindow() method and a showPanel() method.

Fields

Fields represent the fields owned by MeVisLab modules. They are automatically synchronized with the fields of the remote module. MLAB.Core.Module.field() provides access to them.

A field’s value can be retrieved by getValue() and set by setValue(). Listeners can be added to the field and are called whenever the field value changes, due to a field value change on either the server or the client.

Signals and Slots

Signals can be emitted by classes derived from MLAB.Core.Object. Before signals can be emitted, they must be registered first. Signals can be connected to slots, other signals, and callback functions.

// Create a sub class of MLAB.Core.Object.
MLAB.MyPlugin.deriveClass("MyClass", MLAB.Core.Object, {
  MyObjectClass: function() {
    MLAB.MyPlugin.MyClass.super.constructor.call(this)
    this.registerSignal("mySignal")
  },

  doSomething: function() {
    var i = 0
    i += 1
    return i
  },

  emitMySignal: function() {
    this.emit("mySignal")
  }
})

var myObject = MLAB.MyPlugin.MyClass()
var myOtherObject = MLAB.MyPlugin.MyClass()
var callback = function() { }

// Connect the signal.
myObject.connectCallback("mySignal", callback)
myObject.connectSignal("mySignal", myOtherObject, "mySignal")
myObject.connect("mySignal", myObject, "doSomething")

// Emit the signal; now the callback and myObject.doSomething() are called and
// the signal "mySignal" of myOtherObject is emitted.
myObject.emitMySignal()

RequireJS

RequireJS is used for the loading of JavaScript files and modules, as well as for building the single optimized JavaScript file with the full MeVisLab Web Toolkit.

The MWT can be included unoptimized like this:

<script type="text/javascript" src="/Packages/MeVis/ThirdParty/Sources/web/requirejs/require.js"></script>
<script type="text/javascript" src="/Packages/MeVisLab/Standard/Sources/Web/application/js/gui/Bootstrap.js"></script>

Or the optimized file can be included:

<script type="text/javascript" src="/Packages/MeVis/ThirdParty/Sources/web/requirejs/require.js"></script>
<script type="text/javascript" src="/Packages/MeVisLab/Standard/Sources/Web/application/js/MeVisLabWebToolkitCore.js"></script>

The optimized file is typically used in productive environments where performance is an issue. The unoptimized file is better suited for debugging, as the sources are not minified.

Plugins

The main purpose of MWT plugins is that MeVisLab modules can provide JavaScript code for the client. When a module is loaded by the MWT, then it checks if the MDL contains a Web tag. This tag may contain several plugin tags that register the plugins of the module. The Web tag may also include a Deployment tag, which is similar to the top level Deployment tag, except that is only evaluated when building web application installers.

For further information, see MeVisLab Web Toolkit Reference, section Writing Plugins.

Web {
  plugin = "$(MLAB_MeVisLab_Standard)/Sources/Web/application/js/yui/Plugin.js"
  plugin = "$(MLAB_MeVisLab_Standard)/Sources/Web/application/js/jquery/Plugin.js"
  plugin = "$(LOCAL)/www/js/TestPlugin.js"

  Deployment {
    directory = $(LOCAL)/www
  }
}

Remote Rendering

The basic remote rendering concept is described in the Remote Modules overview. The MeVisLab Web Toolkit provides a base field handler, a remote rendering control, and a remote rendering widget to allow for using remote rendering in web applications.

The remote rendering can be configured using MLAB.Core.RenderSettings. The remote rendering control and widget are automatically created by the MeVisLab Web Toolkit when a module window or panel is shown and its MDL description contains a RemoteRendering control. Browser events that occur on the remote rendering widget are delivered to the Open Inventor scene in the worker process.

During scene interaction, the remotely rendered images are by default transferred in JPEG format. If the interaction has stopped, a PNG image is sent. These defaults can be changed by changing the settings.

Remote rendering supports streaming, which is enabled by default. This means that the server does not wait for the client to request an image, but sends images when the scene changes. This reduces the latency problem on the connection, because the server does not need to wait for the image request message. The server stops sending images if a certain number of images has been sent and no acknowledgment for an image has been received from the client. This prevents overburdening the bandwidth or client, whichever is the bottleneck.

Remote Call Interface

The remote call interface allows for sending messages from the server to the client. It uses remote messages over a Base field. The Base object provides the following methods, which are supported by the MeVisLab Web Toolkit:

  • call(method, arguments)

  • callWithResult(method, arguments, userData, callback)

It can be used from the Python context via MLABRemote:

def myCallback(result):
  print result

# Calling a global method
MLABRemote.call("myGlobalMethod", [0, 1, "arg2", {"arg3": ["value0", "value1"]}])
MLABRemote.callWithResult(myCallback, "myGlobalMethod", ["someArgument"])

# Calling a method on the module
MLABRemote.callOnModule("myMethod", [0, 1, "arg2", {"arg3": ["value0", "value1"]}])
MLABRemote.callWithResultOnModule(myCallback, "anyModuleMethod", ["someArgument"])

call() and callWithResult() prepend “::” to the callback method name to indicate that a global method should be called. callOnModule() and callWithResultOnModule() do not prepend this string, and the method is looked up on the module that owns the Base field. This logic is implemented in the MLAB.Core.RemoteCallInterfaceHandler.

Developing Web Applications

The base for all web applications is a MeVisLab macro module. It will be the interface between the server and client. It must include a Python script, which must import the MLABRemote module:

from MLABRemote import MLABRemote, allowedRemoteCall
MLABRemote.setup(ctx)

The MDL GUI description will be automatically sent to the web client using the MLAB.Core.ModuleWindowDefinitionsMessage after the module was created. The MeVisLab Web Toolkit will generate HTML from specified MDL windows. But the MDL control support is only a subset of MeVisLab/Qt implementation. Some controls are not available at all, others support only certain features. See the list of supported MDL controls for details.

The most important MDL control for web applications is probably the RemoteRendering control for viewing images. Web applications may almost completely ignore the HTML from MDL generation (for example, if they use a web framework like Django to generate the HTML UI). The RemoteRendering control is still required, because it is the only web browser implementation of the remote rendering interface of MeVisLab.

If no web framework is used and no custom HTML document is provided, then the only way to add JavaScript logic to the web application is to use a MWT plugin. The default HTML document includes only two JavaScript libraries: RequireJS and the optimized MeVisLab Web Toolkit. (The default HTML document is $(MLAB_ROOT)/MeVisLab/IDE/Configuration/Installers/Shared/Standalone/Web/webApplicationIndex.html.)

A user script for running a macro module as web application is startWorkerService.py in $(MLAB_ROOT)/MeVisLab/Standard/Modules/UserScripts/. It uses a temporary worker service configuration that has the HTTP server of the worker service enabled. The HTML page is opened in the system web browser.

Example Web Application

$(MLAB_ROOT)/MeVisLab/Examples/Projects/MLABReact/README.md provides a showcase of how to develop a small web application using the React framework. It includes guidance on debugging, updating the application during development, and creating an appropriate installer.

Allowed Remote Calls

The MLAB.Core.Module.callRemoteFunction() method allows for calling Python functions of the application macro on the server. Since it is a security hole to allow calling any Python function, these functions must be explicitly allowed. The MDL tag allowedRemoteCalls can be used to restrict access to certain functions. It accepts the name of Python functions as a comma-separated list. By default, this tag is not set and calling any Python function is disallowed. Another way to allow calling Python functions is the decorator MLABRemote.allowedRemoteCall. The MDL tag and Python decorator can be either used alone or together in a web macro module.

Deployment

MeVisLab standalone applications are deployed as installers, which are generated by the installer wizard. A web application installer is also a standalone application installer, but it must define additionally the installer variable WEB_APPLICATION. The standalone application installer wizard provides a selection box on the second page—select “Web Application Installer” to include this variable in the generated installer file.

Specifying that an installer is a web application installer will cause the following:

  • the generated installer will be a zip file that contains an applications and a www directory

  • application files and binaries are placed in applications/$(WEB_APPLICATION_NAME)/

  • files for the web server are placed in www/$(WEB_APPLICATION_NAME)/

  • the module dependency analyzer will evaluate the deployment tags in the Web sections of the module script files (see also the example in section Plugins)

  • the optimized MeVisLab Web Toolkit script file and default CSS style is always included

  • JQuery, JQueryUI, and YUI are included, depending on the WEB_CONFIG variable

  • generation of the HTML page from the template file WEB_APPLICATION_INDEX_TEMPLATE as www/$(WEB_APPLICATION_NAME)/index.html

  • generation of the application configuration file (see the Remote Modules overview) from the template file WEB_APPLICATION_CONFIG_TEMPLATE as applications/$(INSTALLER_WEB_APPLICATION_NAME).xml

Installer Variables

WEB_APPLICATION

must be set in the .mlinstall file to make the installer a web application installer: $WEB_APPLICATION 1

WEB_APPLICATION_CONFIG_TEMPLATE

specifies the template file for the application configuration file, defaults to $(MLAB_MeVisLab_IDE)/Configuration/Installers/Shared/Standalone/Web/ApplicationConfig.xml

WEB_APPLICATION_INDEX_TEMPLATE

specifies the template file for the index.html file, defaults to $(MLAB_MeVisLab_IDE)/Configuration/Installers/Shared/Standalone/Web/webApplicationIndex.html

WEB_APPLICATION_NAME

defaults to $PRODUCT if not defined in the .mlinstall file

WEB_CONFIG

defines if any library of JQuery, JQueryUI, or YUI are included, defaults to (JQUERY|JQUERYUI|YUI)