Skip to main content

How to use Python Script in opnbi

This document contains information on how to setup jupyter notebook server in docker and connect with opnbi application to run server script in python language to fetch data in dataset.

Python script can be called from server script as well as in predict option available in widets.

info

Know more on how to use python prediction model in dashboard Click Here

Note

To run python server script we just need jupyter python kernel and in jupyter you can setup 100+ kernel for different langauage

Requirements

  • Jupyter Notebook

  • System space: 4 GB

  • Sample Project Click Here to donwload

    In code or docker code Attached in requirements contain two server,

  1. Jupyter with R+python kernel to run server side script. it is used to execute server scripts

  2. Contain just main python file which use python model to predict and find outliers. it is used to execute predict APIs

    For the demonstration, we have a python project named opnbi_app and we are using docker to deploy this project. Along with this project, we are deploying jupyter server in docker to execute server script.

i). Using sample project

  1. Download sample project files from link provided in Requirements.

  2. Download docker desktop from official docker website. Click here

  3. For windows, follow steps given in this link to setup docker in windows

  4. After installation completion, Open docker, you will see home screen as below,

    Docusaurus Slash Introduction

  5. Extract sample project and go to specified directory. Here you will find 2 Projects and one .yml file

    Docusaurus Slash Introduction

  6. Open a PowerShell window and change path to specified directory

    Docusaurus Slash Introduction

  7. Run docker-compose up command to build a simple Python web application running on Docker Compose

    Docusaurus Slash Introduction

  8. After running this command, it will download all dependencies required for python project and jupyter notebook. It will take some time to download and start notebook server

    Docusaurus Slash Introduction

  9. Once server is started, open opnbi application in browser and login with valid credentials

    Docusaurus Slash Introduction

  10. After login, click on Hemberger menu and go to opnbi configuration section

    Docusaurus Slash Introduction

  11. In Kernel IP setting Provide IP address 127.0.0.1 instead of localhost, click on save button to save the changes.

    Docusaurus Slash Introduction

  12. Go to dataset section to create dataset

    Docusaurus Slash Introduction

  13. Click on create dataset button from the footer menu

    Docusaurus Slash Introduction

  14. From Datasource dropdown select Empty dataset option. mark server script checkbox given on the top right side of the window. Once you select this checkbox in language dropdown python3 will be selected by default and user will be redirected to server script editor window.

    Docusaurus Slash Introduction

  15. User can write python script here. for the demonstration, we have pasted sample python script in this editor.

    Docusaurus Slash Introduction

    info

    Sample code used here is added below

    dict = {"country": ["Brazil", "Russia", "India", "China", "South Africa"], 

    "capital": ["Brasilia", "Moscow", "New Dehli", "Beijing", "Pretoria"],

    "area": [8.516, 17.10, 3.286, 9.597, 1.221],

    "population": [200.4, 143.5, 1252, 1357, 52.98] }

    import pandas as pd

    brics = pd.DataFrame(dict)

    data = {'data':brics.to_json(orient = 'records')};

    display(data);

  16. Click on preview button to execute script and get the data in preview tab

    Docusaurus Slash Introduction

    info

    If you are not getting data in preview mode, go to opnbi configuration section from Hemberger menu and match Auth Token value with opnbi_TOKEN value available in docker-compose.yml file provided with sample project. If token provided in yml file not match with Auth Token, user need to change token of .yml file as per Auth Token given in opnbi configuration.

  17. Go to general tab and provide a dataset name and click on submit button to save this dataset.

  18. Once dataset saved successfully, user can use this dataset to create visualizations and reports in opnbi.

ii). Using existing jupyter

  1. If you already have jupyter in that case you have to make some changes in jupyter configuration as below;

    c.NotebookApp.token = 'opnbihub' [if you already have token then just configure in opnbi configuration]

    c.NotebookApp.password = '' [if required]

    c.NotebookApp.allow_origin = '*' [as per your requirement]

    c.NotebookApp.disable_check_xsrf = True

    c.NotebookApp.notebook_dir = '/home/jovyan/work' [as per your requirement]

    c.NotebookApp.open_browser = False

iii). Using separate jupyter

  1. If you want separate jupyter in that case please follow below steps;

  2. As per your requirement, user can select required image file for jupyter from below link;

    https://jupyter-docker-stacks.readthedocs.io/en/latest/using/selecting.html

    A. docker pull jupyter/base-notebook:latest 

    B. docker run -it --rm -p 8888:8888 jupyter/base-notebook start-notebook.sh --NotebookApp.token='opnbihub' --NotebookApp.password = '' --NotebookApp.allow_origin='*' --NotebookApp.disable_check_xsrf=True --NotebookApp.open_browser=False

    And it will work directly with opnbi.