Python API

A distinctive feature of Radiant Framework is its ability to execute pure Python code directly from the interface through the Tornado server. This integration facilitates seamless Python scripting within a web-based environment, enhancing the interactivity and functionality of web applications developed using Brython.

To implement this feature, the Python code must be hosted in a separate script, within a class that inherits from PythonHandler. This approach organizes the code efficiently and leverages the capabilities of the Tornado server for Python execution.

[ ]:
from radiant.framework.server import PythonHandler
import math


class MyClass(PythonHandler):

    def local_python(self):
        """"""
        return "This file are running from Local Python environment"

    def pitagoras(self, a, b):
        """"""
        return math.sqrt(a**2 + b**2)

    def test(self):
        """"""
        return True

Next, it’s necessary to configure the main script to load this module. In this case, you should include python=('python_foo.py', 'MyClass') in the arguments of RadiantServer. This setup ensures that the specified Python class is correctly loaded and integrated into the server environment.

This configuration defines how we use the module and the name we assign to it. In this instance, we are using MyClass as the name for our module.

[ ]:
class BareMinimum(RadiantAPI):

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        document.select_one('body') <= html.H1('Radiant-Framework')

        c = self.MyClass.pitagoras(3, 5)

The complete code for this implementation is presented below:

[ ]:
from radiant.framework.server import RadiantAPI, RadiantServer
from browser import document, html

class BareMinimum(RadiantAPI):

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        document.select_one('body') <= html.H1('Radiant-Framework')

        c = self.MyClass.pitagoras(3, 5)

if __name__ == '__main__':
    RadiantServer(
        'BareMinimum',
        python=('python_foo.py', 'MyClass'),
    )