# Basics to get started

[**Xsession():**](https://docs.xplain-data.de/xplaindoc/interfaces/xplainpy/references/xplainsession.html#xplain.Xsession) login to an Object Analytics Instance and init a Xsession instance&#x20;

```python
from xplain import Xsession
s = Xsession(url="http://myhost:8080", user="me",password="secret1")

```

[**Xsession.startup(file\_name)**:](https://docs.xplain-data.de/xplaindoc/interfaces/xplainpy/references/xplainsession.html#xplain.Xsession.startup) load a XView (xstartup) file to setup a new xplain session

```python
from xplain import Xsession

s = Xsession(url="http://myhost:8080", user="me",
password="secret1")
s.startup("myXview")
```

{% hint style="info" %}
If you use the xplain python package locally in your python environment, then you do need to setup a startup. \
If you use the xplain python package at XOE in ObjectExplorer, then you do not need to setup a startup.&#x20;
{% endhint %}

[**Xsession.load\_from\_session\_id(session\_id)**](https://docs.xplain-data.de/xplaindoc/interfaces/xplainpy/references/xplainsession.html#xplain.Xsession.load_from_session_id)**:** load xplain session by given exisiting session id, in order to enable shared session between python and Object Analytics

<div><figure><img src="/files/kww7FWIO3766tMi3yDpH" alt=""><figcaption></figcaption></figure> <figure><img src="/files/vWMvN6b32EvxA0jcrU8S" alt=""><figcaption><p>get the session id from Object Analytics</p></figcaption></figure></div>

```python
import xplain

x = xplain.Xsession()
x.load_from_session_id("C8DCE9C450ED83FF4E679EE840A072FF")
```

[**Xsession.execute\_query()**: ](https://docs.xplain-data.de/xplaindoc/interfaces/xplainpy/references/xplainsession.html#xplain.Xsession.execute_query)execute a query

<pre class="language-python"><code class="lang-python">import xplain

s = xplain.Xsession()
<strong>query = {
</strong><strong>        "requestName": "myRequest",   
</strong>        "aggregations" :[
            {
               "object": "AU-Diagnosen",
               "dimension": "Diagnose",
               "type": "SUM"
           }
        ],
        "groupBys":[{
           "attribute": {
               "object": "Hospital Diagnose",
               "dimension": "Diagnose",
               "attribute": "Type"
           }]
    }
}
df = s.execute_query(query)
</code></pre>

[**Xsession.open\_query()**](https://docs.xplain-data.de/xplaindoc/interfaces/xplainpy/references/xplainsession.html#xplain.Xsession.open_query)**:** execute an <mark style="color:orange;">open query</mark>&#x20;

```python
import xplain

s = xplain.Xsession()
query = {
        "requestName": "myRequest",   
        "aggregations" :[
            {
               "object": "AU-Diagnosen",
               "dimension": "Diagnose",
               "type": "SUM"
           }
        ],
        "groupBys":[{
           "attribute": {
               "object": "Hospital Diagnose",
               "dimension": "Diagnose",
               "attribute": "Type"
           }]
    }
}
df = s.open_query(query)
```

[**Xsession.run()**](https://docs.xplain-data.de/xplaindoc/interfaces/xplainpy/references/xplainsession.html#xplain.Xsession.run)**:** To run a xplain method

perform xplain [web api method](https://docs.xplain-data.de/xplaindoc/interfaces/webapi.html) and broadcast the change to other client sharing with same session id, if Python session is sharing the same session with Object Analytics, Object Analytics will be get updated.

```python
x=xplain.Xsession()
x.run({"method":"clearAllSelections"})
```

[**Xession.perform():** ](https://docs.xplain-data.de/xplaindoc/interfaces/xplainpy/references/xplainsession.html#xplain.Xsession.perform)Send POST request against entry point /xplainsession with payload as json, returns results as JSON

```python
x=xplain.Xsession()
teenager_list = r.perform({
    "method": "getChildren",
    "object": "Patient",
    "dimension": "Age",
    "attribute": "Age Group",
    "state": "Teenager"
})
```

[**Xsession.get\_result(query\_name):** ](https://docs.xplain-data.de/xplaindoc/interfaces/xplainpy/references/xplainsession.html#xplain.Xsession.get_result)get the result of a given query name as data frame.

```python
import xplain

s = xplain.Xsession()
query = {
        "requestName": "myRequest",   
        "aggregations" :[
            {
               "object": "AU-Diagnosen",
               "dimension": "Diagnose",
               "type": "SUM"
           }
        ],
        "groupBys":[{
           "attribute": {
               "object": "Hospital Diagnose",
               "dimension": "Diagnose",
               "attribute": "Type"
           }]
    }
}
s.execute_query(query)

df = s.get_result("myRequest")
```

## 3 different ways to perform a query

Following example shows how to count I**tems** grouped by **Item Type** where **Item Category** = 10.0 in 3 different ways

References:

[xplain.Query\_config](https://docs.xplain-data.de/xplaindoc/interfaces/xplainpy/references/query_config.html#)

```python
'''
This example demostrate 3 different ways to execute a query
to count Items groupped by Item Type where Item Category = 10.0
'''
import xplain

x = xplain.Xsession(url="https://xoebp.xplain-data.com")


## Option 1: in object oriented way with query object 
# step 1: config the query 
my_query = xplain.Query_config(name="myExample")

my_query.add_aggregation(object_name="Items", dimension_name="Item ID", type="COUNT")

my_query.add_groupby(object_name = "Items", 
                    dimension_name = "Item Type", 
                    attribute_name="Item Type")

my_query.add_selection(object_name = "Items", 
                       dimension_name = "Item Category", 
                       attribute_name="Item Category",
                       selected_states=["10.0"])

# step 2: run query with query config object
x.execute_query(my_query)
print(my_query.to_json())

## Option 2. run with execute_query method and use query config JSON as parameter
x.execute_query({
    'requestName': 'myExample2', 
    'aggregations': [{
        'object': 'Items', 
        'dimension': 'Item ID', 
        'type': 'COUNT'      
        }], 
        'groupBys': [
            {
                'subGroupings': 
                    [
                    {
                        'attribute': 
                        {
                            'object': 'Items', 
                            'dimension': 'Item Type', 
                            'attribute': 'Item Type'
                        }
                    }
                    ]
            }], 
            'selections': [
                {'attribute': 
                    {'object': 'Items', 
                    'dimension': 'Item Category',
                    'attribute': 'Item Category'}, 
                    'selectedStates': ['10.0']}
                    ]
    })




## Option 3: use run method and xplain Web API 
# https://docs.xplain-data.de/xplaindoc/interfaces/webapi.html
# https://docs.xplain-data.de/xplaindoc/interfaces/webapi.html#executequery

x.run({  
    "method": "executeQuery",
    "request": {
    'requestName': 'myExample3', 
    'aggregations': [{
        'object': 'Items', 
        'dimension': 'Item ID', 
        'type': 'COUNT'
            
        }], 
        'groupBys': [
            {
                'subGroupings': 
                    [
                    {
                        'attribute': 
                        {
                            'object': 'Items', 
                            'dimension': 'Item Type', 
                            'attribute': 'Item Type'
                        }
                    }
                    ]
            }], 
            'selections': [
                {'attribute': 
                    {'object': 'Items', 
                    'dimension': 'Item Category',
                    'attribute': 'Item Category'}, 
                    'selectedStates': ['10.0']}
                    ]
    }
})

```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://xplain-data.gitbook.io/community-edition/python-services-overview/xplain-python-package/basics-to-get-started.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
