The table below explains the above code snippet.
Line of Code | Result |
| This custom function checks the type of the dataset (Excel, CSV, and Text) and accordingly reads the dataset and returns it as an output of the function of dictionary type. |
| Prints the Reader output data as a dictionary to the console log or custom component log. |
Note: | In the |
A sample Python code to write data to file is shown in the image below.
In the above code, the writeDataToFile custom function is used to append a row to the selected Carbo Fitness file (dataset).
dataToWrite
stores the output data of the type dictionary to write to a file.writeDataToFile(dataToWrite,“action”,“delimiter”,“datasetName”)
custom function appends or overwrites the output data to the selected file (Excel, CSV, or Text datasets).The table below describes the writeDataToFile
function and parameters.
Function | Parameter | Remarks |
|
| — |
| It is of type String, and values can be overwrite and append. | |
| It is of type String, and values can be – “,” / “|” / “ ” / “ ” (comma, pipe, tab, and space). | |
| It is of type String. |
Another example of reading and writing to a file is shown below.
You can use getReaderData
to read the data from RDBMS Table.
In the above code,
getReaderData(“datasetName”,“subdatasetName”)
custom function checks the type of the dataset and accordingly reads the dataset from the Reader and returns it as an output of the function of type dictionary.print2log(dataToWrite)
prints the output data as dictionary to the console log or custom component log.
Note: | In the getReaderData custom function, the dataset and the sub-dataset names are the same for all datasets except for RDBMS datasets. In case of RDBMS datasets, sub-dataset name is the name of the table added in the RDBMS dataset. |
Similarly, you can use writeDataToTable
to write the data into RDBMS Table.
The table below describes the writeDataToTable
function and parameters.
Function | Parameters | Remarks |
|
| — |
|
| |
| It is of type String. | |
| It is of type String. | |
|
| |
|
|
Notes: |
|
RubiPython provides a custom function to upload files to S3 server. The code syntax to upload a file to cloud storage is shown below.
|
RubiPython provides a custom function to download files stored on S3 server. The code syntax to download a file from cloud storage is shown below.
|
If the downloaded file is a CSV, you can use the below syntax to read the contents of the downloaded file.
|
The "getConnectionObject" is a custom function in RubiPython. It allows access to the database without attaching the reader node to the RubiPython. It provides the single encrypted connection object in all python scripts.
To get the connection with the database, follow the steps given below.
Notes |
|
A sample Python code to make the connection to the database is shown below.
The table below explains the above code snippet.
Line of Code | Result |
import pandas as pd | Imports the Pandas library into the current Python script or and assigns it the alias "pd" |
conn = getConnectionObject("SalePy") | It creates a connection to a "SalePy" dataset. |
query = 'SELECT * FROM public."SaleData" ' | It holds a SQL query, which selects all columns ( * ) from a table named "SaleData" in the "public" schema. |
df = pd.read_sql(query, con=conn) | Uses the pd.read_sql() function from the pandas library to execute the SQL query and retrieve data into a DataFrame. |
print2log(df.head(10)) | Prints the first 10 rows of the DataFrame 'df' to the log. |
In addition to the functionality, you have the capability to Insert, Update, or Delete the row within an existing dataset. For more info, refer the sample code snippets.
The variables defined at the workbook/workflow level can be used in the RubiPython custom component.
To use a user-defined variable in RubiPython, follow the steps given below.
You can publish the RubiPython code from a workbook and reuse it in another workbook or workflow. This feature is similar to publishing models in RubiStudio.
To publish RubiPython code, follow the steps given below.
After the code is successfully published, a confirmation message is displayed. This code is listed under Reusable Codes on the Rubiscape Home page.
Notes: |
|
The published RubiPython code is available for reuse in workbooks and workflows in the same workspace.
To reuse a published code, follow the steps given below.
The available reusable codes are displayed as shown in the figure below.
After the node executes successfully, a confirmation message is displayed.
Using the 'getFileFromServer' custom function, you can now read data from a file on the Rubiscape File Server and access it directly in the RubiPython and RubiNotebook node without creating a dataset.
A sample code snippet to get the file from server in RubiPython is provided below:
Line of Code | Result |
a = getFileFromServer("/Bank.xlsx") | 'getFileFromServer' is a function, which retrives the file 'Bank.xlsx' from the rubiscape file server and assigns it o variable 'a'. |
import pandas as pd | Imports 'pandas' libraries in python for data manipulation and ;BytesIO' from 'IO' to handle binary data. |
data = BytesIO(a.getvalue()) | In this line creates an in-memory binary stream using BytesIO from the binary content of a. The getvalue() method is called on a to retrieve the binary content. |
df = pd.read_excel(data) | Reads the excel file from the 'data' and streams into pandas dataframe. |
print2log(df.head(5)) | Prints the first five rows of the DataFrame df to the log using a custom function print2log(). |
return df | Returns the DataFrame df, which contains the data from the Excel file. |
After successfully running the RubiPython node, the output variables from the data will be available in the Custom Output Variables.
Similarly, a sample code snippet to get the file from server in RubiNotebook is provided below: