Skip to content

redpesk-samples/helloworld-python-binding

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

helloworld-python-binding

A Python version of helloworld-binding example, demonstrating how https://github.com/redpesk-common/afb-libpython can be used to implement an AFB binding in Python.

Exposed API and verbs

The "helloworld" binding exposes an API called helloworld with the following verbs:

  • ping: takes no argument and returns a "pong" message
  • testargs: takes one JSON argument and tests its internal structure

Run the binding in a non-embedded environment

Since this helloworld binding is not connected to real hardware, it can be run and tested on a Linux workstation.

In order to do so, you will need to install afb-libpython. It is available in redpesk SDK.

Please follow first the SDK installation documentation if you have not done so before, and if your platform is supported.

Install then Python bindings on your platform thanks to your distribution's tools: e.g. sudo apt install libafb-python.

You should then be able to run helloworld-binding with: python3 helloworld_python_binding/helloworld-binding.py

RPM Build

The aim of this part is to create a package which contains the helloworld-python-binding application.

Specfile

In order to correctly build an RPM package in the redpesk context, your project should contain a specfile. The helloworld-python-binding specfile can be found at the following path: conf.d/packaging/helloworld-python-binding.spec. This specfile will be used by redpesk to describe how to build your helloworld binding application. The resulting build produces a helloworld widget that is embedded in a RPM. Please read the documentation to correctly create and set your redpesk project and application set up.

Deploy on a board

Now that a helloworld-python-binding package has been generated thanks to redpesk, let's see how to install it on a board. Since redpesk supports cross-compilation, you can use either an x86_64 or an aarch64 board to get your package installed in. During this part, one case will be studied:

  • Emulated x86_64 board: qemu-x86_64

If you want to use the helloworld-python-binding in an embedded aarch64 context, but do not have a real board, another tutorial is present at the following path.

QEMU installation and configuration

Please refer to the official documentation to download a redpesk image and run it through QEMU.

redpesk factory application setup

In order to use redpesk factory, you will need an account on our free instance: https://community-app.redpesk.bzh

You can then follow the guide from the documentation that explains how to add an application.

Use the provided specfile and it should be able to build an RPM package of helloworld-binding ready to be installed on your board.

Package deployment

Refer to the official documentation that explains how to add a new RPM repository that contains your package to the board.

Once the repository has been added, the package can be installed with sudo dnf install helloworld-python-binding

Run the binding

$ python3.6 /usr/lib/python3.6/site-packages/helloworld_python_binding/helloworld-binding.py
Entering Python module initialization function PyInit_libafb
   NOTICE: Entering binder mainloop
   NOTICE: loop_binder_cb=helloworld-python

Use the binding on the command line

PING Test

In another terminal, via ssh, you can access to its verb ping which belong to its api helloworld thanks to a HTTP or websocket request sent to the port 1234 such as follow

tip: for a better display of the request response, install jq (e.g. with: dnf install jq) and use it with | jq like in the following command

$ curl -s http://localhost:1234/api/helloworld/ping | jq

You should get the following answer from the request

{
  "jtype": "afb-reply",
  "request": {
    "status": "success",
    "code": 0
  },
  "response": {
    "pong": 1
  }
}

Arguments Test {"cezam" : "open"}

$ curl -s -H "Content-Type: application/json" http://localhost:1234/api/helloworld/testargs -d '{"cezam" : "open"}' | jq
{
  "jtype": "afb-reply",
  "request": {
    "status": "success",
    "code": 0
  },
  "response": {
    "code": 123456789
  }
}

Use the binding with Binder Devtool's web interface

In another terminal, set the port forwarding with ssh

ssh -L 1234:localhost:1234 root@localhost

Then with your favorite web browser, access the Web UI with this url :

localhost:1234/devtools

About

A binding example for python

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 2

  •  
  •  

Languages