Getting Started with Python
Example Source Code

Python

Github files referenced:

Configuration/refinitiv-data.config.json

Quick Start/QS_1.0 - Sessions.ipynb

Last Update June 2024
Interpreter Python 3.11.x or greater
Prerequisites

Familiarity with Python and a basic understanding of Jupyter Notebook.

Access Credentials

If accessing content from:

  • Desktop (Eikon 4.0.36 or greater / LSEG Workspace 1.8 or greater) 
  • Deployed Environment (ADS 3.2 or greater)

The majority of the examples are built to work within Jupyter Notebook.  Ensure this package is installed.

The goal of this Getting Started tutorial is to provide the Python developer with the steps required to prepare a development environment to access content within the LSEG Data Platform. As outlined within the LSEG Data Library for Python - Reference Guide, the libraries are designed as ease-of-use interfaces, available in multiple programming languages, allowing the developer a consistent way to access content from multiple supporting access points. That is, they can choose to access content from either the Eikon or LSEG Workspace desktop, directly to the cloud (LSEG Data Platform/Real-time Optimized) or via a locally deployed enterprise installation (RTDS/ADS).

This quick start guide will provide the steps to set up and configure the Python Jupyter Notebook example package necessary to access the content.

Install the LSEG Data Library for Python

Run the following command to install the library package from the Python Package Index:

    	
            > pip install lseg.data
        
        
    

Configure and run examples

The LSEG Data Platform provides a number of data services such as streaming, request/reply, alerts and bulk files.  Depending on your specific requirements, an example package will be available for you to verify access.  The downloadable example package will provide a series of examples that will cover specific capabilities that will support a cross-section of the services within the platform.

After downloading and extracting the Python Examples, start your Jupyter Notebook and load the package:

    	
            > jupyter notebook
        
        
    

The package will present examples within Quick Start and Tutorials folders.  Prior to running any example, you will need to define your credentials in order to access the content.

Before assigning your access credentials within the Configuration/lseg-data.config.json file included within the example package, ensure you have reviewed the Access Credentials Guide.


NOTE
: Do NOT modify the internal configuration file that is installed with the library (e.g in the site-packages\lseg\data folder) - modify your own local copy - i.e the one in the Configuration folder of the Examples package (or your choice of directory - when you start writing your own code).

In the Credentials file, you need to set your AppKey regardless of which Session (connection type) you intend to use.

  • If you will be connecting to the LSEG Data Platform via Version 1 OAuth2 ('Password Grant'), then you also need to set the LDP_LOGIN and RDP_PASSWORD
  • If you will be connecting to the LSEG Data Platform via Version 2 OAuth2 ('Client Credentials'), then you also need to set the "app-key", "client_id" and "client_secret" (as shown under "ldpv2auth" in the image below). Note that if you are connecting with 'Version 2 OAuth2', you can authenticate yourself to LSEG's services in-line/in-code using
    	
            

session = nd.session.platform.Definition(

    app_key='app_key',

    app_secret='app_secret',

    client_id=nd.platform.ClientCredentials(

        client_id='client_id',

        client_secret='client_secret'),

    signon_control=True

    ).get_session()

session.open()

return session

  • For a Deployed (ADS) connection, you need to provide the hostname:port of the ADS as well as a Dacs Username
  • For a Desktop (Eikon/Workspace) connection, only the above AppKey is required

(see the Access Credentials Guide for further details)

Default Session

In the lseg-data.config file you can specify a default session, so that whenever you call ld.open_session() without any parameters, the LD library will attempt to open that session.
For example, in the config below, I have specified the 'desktop' session named 'workspace' as the default. So, when ld.open_session() is called, the 'AppKey' in that section will be used to create a Desktop Session.

If you are not connecting to a desktop product (Eikon or LSEG Workspace) and connecting for example, to the LSEG Data Platform - then you would set your default to "platform.ldp" to use your LDP credentials instead.

    	
            

{

  "logs": {

      "level": "debug",

      "transports": {

          "console": {

              "enabled": false

          },

          "file": {

              "enabled": false,

              "name": "lseg-data-lib.log"

          }

      }

  },

  "sessions": {

      "default": "desktop.workspace",

      "platform": {

          "ldpv1auth": {

              "app-key": "YOUR LDP (V1 OAUTH) APP KEY GOES HERE",

              "username": "YOUR USER ID OR MACHINE ID GOES HERE",

              "password": "YOUR LDP (V1 OAUTH) PASSWORD GOES HERE"

          },"ldpv2auth": {

              "app-key": "YOUR V2 OAUTH APP KEY GOES HERE",

              "client_id": "YOUR SERVICE ID GOES HERE",

              "client_secret": "YOUR CLIENT SECRET GOES HERE"

          },

          "deployed": {

              "app-key": "YOUR APP KEY GOES HERE!",

              "realtime-distribution-system": {

                  "url": "ws://YOUR DEPLOYED HOST:PORT GOES HERE",

                  "dacs": {

                      "username": "YOUR DACS ID GOES HERE",

                      "application-id": 256,

                      "position": ""

                  }

              }

          }

      },

      "desktop": {

          "workspace": {

              "app-key": "YOUR LDP (V1 OAUTH) APP KEY GOES HERE"

          }

      }

  }

}

NOTE: If you are using a Deployed server (e.g. ADS) you will need to provide:

  • the server's hostname and Websocket API Port e.g. 'ws://ADS1:15000' - where ADS1 is your server's hostname
  • your DACS ID e.g. 'umer.nalla'
  • optionally you can also specify an Application ID - default 256

Also, you must bear in mind that at present, a Deployed connection can only deliver real-time data - so most of the other tutorials and examples provided such as historical data, symbology, news etc will not return any data - when used with a Deployed session.

Define a session using your own credentials store

Alternatively, if you do not wish to use a lseg-data config file, you can define sessions by specifying the parameters and session type explicitly. Sessions can be created by explicitly passing in your credentials/connectivity parameters as shown further down in the QS_1.0 - Sessions notebook.
The most immediate choice may be to hardcode the credentials into your notebook or script. However, this risks accidentally exposing your credentials when sharing your code with others. Therefore, Instead of putting your credentials in the notebook file, one option could be to use a standalone .env file as demonstrated in the QS_1.0 - Sessions notebook (a sample .env is also included on Github in the Quick Start folder).

Testing your credentials

Once the config file has been populated, select the QS_1.0 - Sessions notebook in the Quick Start folder. 

Note: If you choose to access content using a Desktop connection within the selected example,  the Eikon or LSEG Workspace application must be running. When launched, the Eikon or LSEG Workspace application starts listening for local connections on port 9000 or the next available port if port 9000 is already occupied.

  1. Load the tutorial within Jupyter (or other IDE)
  2. Step through the code - executing the relevant code cells as per your credentials i.e. Desktop, Platform or Deployed

When you execute the rd.open_session() cell, you should see output such as:

<lseg.data.session.Definition object at 0x2a1fc4b9e80 {name='workspace'}>

Troubleshooting

If you do not see above or see State: Closed, or some other error the most likely cause of failure would be:

  • The details you provided within the lseg-data.config.json file may be incorrect
  • or for a Desktop Session, your Eikon or Workspace is not running on the same PC

If you have confirmed that the above scenarios do not apply, you can help diagnose the issue, by editing the lseg-data.config.json file and enabling either console or file logs and then running the steps again.

    	
            

   "logs": {

        "level": "debug",

        "transports": {

            "console": {

                "enabled": true

            },

            "file": {

                "enabled": false,

                "name": "lseg-data-lib.log"

            }

        }

In the above example, I have enabled console logging - to log directly into the notebook cell - you can use the file option instead if preferred.

You can then copy the console log output / log file and share that with the LSEG Support team (or post on the Q&A Forum - after removing any credential information)

 

Next Steps

Once access to the platform has been verified, we recommend you work through the tutorials to better understand the capabilities and power of the LSEG Data Platform Libraries.  

In addition to these tutorials, you can also refer to the additional examples on GitHub