Scilab/C4/Developing-Scilab-Toolbox-for-calling-Python-and-its-functions/English
|
|
Slide 1: | Welcome to the spoken tutorial on Developing Scilab Toolbox for calling Python and its functions |
Slide 2:
Learning Objectives |
In this tutorial we will learn about how to
|
Slide 3:
Systems Requirements |
To record this tutorial, I am using
|
Slide 4:
Pre-requisites |
To practice this tutorial, you should have basic knowledge of programming in
If not, then go through the relevant tutorials on this website. |
Slide 5:
Software prerequisite: |
Make sure that you have installed the below software.
Note that your Linux OS version should be 18.04 and above. |
Slide 6:
Scithon Toolbox
|
In this tutorial, we will call a Scilab Python Interfacing toolbox by the name Scithon.
Scithon toolbox is used to call the Python functions inside Scilab. |
Slide 7:(a)
Additional reading material |
Refer to the Additional reading material link for
1. Installation of Python 3.8 on Ubuntu Linux OS 2. Reference material for the procedure to make the Scithon toolbox. 3.About gateway functions used in this tutorial
|
Slide 7:(b)
Additional reading material |
Please read the document to understand the concepts of
|
I’ll show a demo with a sample Scithon toolbox which is already created. | |
Point to the folders and files as per narration | I have saved scithon.zip and header.zip files in the folder Scilab_hackathon.
Extract the files and keep it in the same folder. Note that header.zip file is required only for Linux users. The Linux version of Scilab requires some additional header files. |
Slide 8:
Code files: |
|
Open the header folder | Let us see the content of the header folder.
This header folder contains two hxx files: OptionalDecoration.hxx and DollarInfo.hxx. It has a folder named data, containing three hxx files. They are Clone.hxx, LoopDecoration.hxx and VarPromotion.hxx |
Slide 9(a):
About header folder: |
|
Slide 9(b):
About header folder: |
|
Go to the header folder
Copy the files |
Go to the header folder.
Copy the 2 hxx files and data folder. We need to paste in the Scilab directory. |
Go to the header folder
Copy the files Show a demo |
I have downloaded Scilab 6.1.0 in my Downloads folder.
Go to : Downloads then scilab-6.1.0 then include then scilab directory. Paste the copied files. |
Slide 10:
Scilab 6.1.0 - apt-get command |
If you are using Scilab 6.1.0 install via apt-get command.
Copy these files inside /usr/include/scilab directory. |
Slide 11:
BlockDiagram1.png |
This diagram shows the interfacing between Scilab and Python using “Python API for C/C++”
This may apply to most interfaces between Scilab and any open source software/library. |
We will see the detailed representation of the Scithon toolbox. | |
Slide 12:
BlockDiagram2.png point to various blocks |
The top block represents code that uses Python API for C/C++.
The middle block represents gateway functions. This transfers data between Scilab and Python API code. The bottom block represents Scilab. In our case, we are particularly making use of the data types available with Scilab. |
Next we will see about the important directories and files involved. | |
Double click on the src directory | Go to the Scithon folder.
Double click on the src folder. |
Point to the sub directories | Here we can see C and CPP folders.
It contains files used for starting the Python instance and overloaded virtual functions. |
open the file PythonInstance.c | Let us open the file PythonInstance.c in the C folder.
The code for creating Python Instances is written by the toolbox developers. This file sets up a way to capture the Python console output. It then starts an instance of the Python interpreter. Close the file. |
Point to cpp
Open the file PyVar.cpp |
Go to the CPP folder.
Open the file PyVar.cpp This file contains the overloaded virtual functions written by the Scithon developers. It defines the behaviour of the custom Python data type used in the toolbox. |
Highlight according to narration
Highlight hasToString, toString, extract, isInvokable and invoke. |
In PyVar.cpp, we have added a data member for storing our Python variable.
This data member will hold the actual Python data with the type as PyObject Next step is to overload the virtual functions that make up a Scilab data type. For example, hasToString, toString, extract, isInvokable and invoke functions are overloaded. |
Python.h is available under
/usr/include/python3.8 |
It uses Python’s extended and embedded API for C/C++.
This is provided by the header file Python.h. We can find this header file under the directory /usr/include/python3.8 |
Open the file PyVar.hxx and highlight user.hxx
user.hxx header file is available under your_scilabinstalltiondirectory/include/Scilab/ |
Let us open the file PyVar.hxx which is written by the Scithon developers.
This is available under the directory scithon/src/cpp/ This implements all the core functionalities that makes up a Scilab data type. This is done by class UserType provided by the header file user.hxx. We can find this header file under the directory scilabinstalltiondirectory/include/Scilab/ |
Slide 13:
Reference Links : PyVar.CPP |
Please refer to the below links to understand the code inside the PyVar.CPP file.
|
Go to C and CPP folder | C and CPP directory contains a builder script of Scithon toolbox.
This is called during the toolbox build process. |
Point to .sce files in the c and cpp folder | The C directory contains the builder_c.sce file.
And the CPP directory contains the builder_cpp.sce file. These files are available in the Scilab toolbox development skeleton. |
Slide 14:
Builder script of scithon toolbox. |
We have downloaded this from scilab.org and modified it to build the Scithon toolbox for Linux.
These scripts compile the C or CPP code and generate shared object files. These shared object files will be used by the Scithon toolbox. Then Scilab will call the implemented functions. |
Next we will see about gateway functions. | |
Go to the sci_gateway directory | Go to the sci_gateway directory in Scithon folder. |
Go to the folder cpp. | Go to the cpp folder.
This directory contains gateway functions required for the toolbox. This acts as an interface between Scilab and Cpp through which interface is made with Python. |
Point to the functions | The Scithon toolbox has 11 functions and 1 overloaded function.
New functions such as py, pyDict, pyEquals etc are created to call the Python utilities in Scilab. We need to create individual gateway functions for each function as shown here. These are the functions that we want to interface with Python. |
Refer to the Additional reading material link for each of the gateway functions. | Refer to the Additional reading material link for more details on each of the gateway functions. |
Open the builder_gateway_cpp.sce
highlight the lines 20 to 23 highlight the lines 6 to line 16 for function names and lines 28 to 38 for gateway files. |
Let us open the builder_gateway_cpp.sce file found in the same folder.
This is used to configure the build process. It contains settings for where to find the headers and libraries. It also contains the list of function names and the corresponding gateway files. |
Next we will see how to build the toolbox. | |
Open the terminal | Open the terminal by pressing Ctrl+Alt+ t keys simultaneously. |
Type sudo updatedb
Type locate libpython3.8.so Highlight the path /usr/lib/python3.8/config-3.8-x86_64-linux-gnu |
Type sudo updatedb.
Enter the system password if prompted. Type locate libpython3.8.so and press Enter. This will give you a search result for where this file is located on your system. I’m getting the path as shown here. Copy the location of the file. |
Open the Scithon/etc folder and open the file scithon.start | Open the scithon/etc folder and open the file scithon.start
Locate the line for link function and check for the file path. If the path is different, then paste the path which we copied earlier. Save and close the file. |
Now let us execute the Python functions in Scilab. | |
Open scilab 6.1.0 | Open Scilab 6.1.0 |
click on the File menu>> Browse for new | In the menu bar, click on the File menu and click on the Browse for new option. |
Browse to Scithon directory | In the new window, browse to Scithon directory.
And click on the Ok button. |
type exec builder.sce | On the Scilab console, type exec builder.sce and press Enter.
This will build the toolbox. Wait until it is completed successfully. |
type exec loader.sce a | Now type exec loader.sce and press Enter.
This will load the toolbox. |
Type help and press Enter.
select Scithon |
Type help and press Enter. The Help window will open.
On the left panel, select Scithon from the list. |
Point to the functions. | On the right side panel, we can see the functions offered by this toolbox. |
In order to start using this toolbox, first execute the command startPy. | |
type startPy | Switch to the Scilab console and type startPy and press Enter. |
Now let us see how to do simple Python computation using this toolbox. | |
py("a = 50; b = a**2;print(b)") | In the Help window, click on the function py.
Copy the example code as shown and paste it in the Scilab console.
|
Point to the output | The py function is used to execute simple Python statements. |
Highlight the output | Notice that the input statement is of type string as well as the output. |
Click on pyList | Switch to the Help window.
Click on pyList |
X = pyList(1,2,"Hello",3) | The pyList function is used to create Lists.
Copy the following code and paste it in the Scilab console and press Enter. You can see that it has created the list X. |
Type X.append(10) | Type X.append(10)
Type X to display the output. It shows that the list has been appended with a new member. |
Point to pyList , append | Note that the pyList() function is written by Scithon toolbox developers to create a Python list.
Append() function is an inbuilt Python function. |
The toolbox facilitates the basic interface of Scilab with Python.
The users are encouraged to explore the remaining functions in the Help window. | |
With this we come to the end of this tutorial
Let us summarize. | |
Slide:6
summary |
In this tutorial we learnt to
|
Slide 7:
About Spoken Tutorial project |
The video at the following link summarises the Spoken Tutorial project.Please download and watch it |
Show Slide:FOSSEE Forum | For any general or technical questions on Scilab, visit the FOSSEE forum and post your question. |
Show Slide:
Textbook Companion project. |
The FOSSEE team coordinates the Textbook Companion project.
We give Certificates and Honorarium to the contributors. For more details, please visit this site. |
Show Slide:
Lab Migration |
The FOSSEE team coordinates the Lab Migration project.
For more details, please visit this site. |
Slide 9:
Acknowledgement |
Spoken Tutorial project is funded by MHRD, Government of India. |
This is Nirmala Venkat from Spoken Tutorial project, IIT Bombay, signing off.
Thanks for watching. |