version

Übersicht: Blend4Web


Objectives

For interactions between the user and the objects represented in the canvas, is the use of JavaScript inevitable. Would't it be nice to use Python in the browser? There are such attempts and with the simplest case of the manual we will demonstrate how it is possible. The usage for more complex tasks is probably not recommended :-(

Instructions

Tasks:
  • Create a test folder and copy b4w.min.js and brython.js into this folder.
  • Save the presented scripts in the test folder.
  • Test the scripts on the browser.

The first version is taken directly from the manual and shows the rationale:

  • Integrating b4w.min.js
  • Definition of a function (here in the head element)
  • And calling a function (here with the onload event)

This has now but nothing to do with Python, what really is regrettable.

blend4web/brython-and-blend4web/hello-js.html (Source)

1
2
3
4
5
6
7
8
9
<!DOCTYPE html>
<html>
<head>
<script src="b4w.min.js"></script> <script>
function hello() {
var m_version = b4w.require("version");
document.body.innerHTML = "Hello, Blend4Web " + m_version.version() + "!"; }
</script> </head>
<body onload="hello()"></body> </html>

Why not use Python in the browser. There are some attempts. The Brython project should be mentioned at this point. It is a Python implementation in JavaScript. Ideally the programmers writes Python code and Brython is transforming the source code to JavaScript. Here is an example with a mix of JavaScript and Python. Hopefully more will be possible in the near future ...

Hints:

In addition to b4w.min.js you need also the Javascript library brython.js and for the Python block the scripttype text/python must be set.

More infos are availabe on the Brython website

blend4web/brython-and-blend4web/hello-py.html (Source)

<!DOCTYPE html>
<html>
<head>
  <script src="b4w.min.js"></script>
  <script src="brython.js"></script>
  <script type="text/javascript">
    var m_version = b4w.require("version");
  </script>

  <script type="text/python">

from browser import window
from browser import document as doc

def hello():
    version = window.m_version.version()
    #print(type(version))
    doc <= "Hello, Blend4Web %s,%s!" % (version[0], version[1])

hello()
    </script>
</head>
<body onload="brython(1)">
</body>
</html>

Comments