Metadata-Version: 2.1
Name: kudaf_jupyter_server_extension
Version: 0.1.1
Summary: Kudaf Data Consumer Jupyter server extension for JupyterLab. It provides the basic infrastructure to interact with the Kudaf system
Home-page: https://gitlab.sikt.no/kudaf/jupyterlab
License: MIT
Author: Daniel Díguele
Author-email: daniel.diguele@sikt.no
Requires-Python: >=3.10,<4.0
Classifier: License :: OSI Approved :: MIT License
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Requires-Dist: aiodns (>=3.1.1,<4.0.0)
Requires-Dist: aiohttp (>=3.9.3,<4.0.0)
Requires-Dist: aiohttp-debugtoolbar (>=0.6.1,<0.7.0)
Requires-Dist: aiohttp-jinja2 (>=1.6,<2.0)
Requires-Dist: aiosignal (>=1.3.1,<2.0.0)
Requires-Dist: anyio (>=4.2.0,<5.0.0)
Requires-Dist: argon2-cffi (>=23.1.0,<24.0.0)
Requires-Dist: argon2-cffi-bindings (>=21.2.0,<22.0.0)
Requires-Dist: arrow (>=1.3.0,<2.0.0)
Requires-Dist: asttokens (>=2.4.1,<3.0.0)
Requires-Dist: async-lru (>=2.0.4,<3.0.0)
Requires-Dist: async-timeout (>=4.0.3,<5.0.0)
Requires-Dist: attrs (>=23.2.0,<24.0.0)
Requires-Dist: authlib (>=1.3.0,<2.0.0)
Requires-Dist: babel (>=2.14.0,<3.0.0)
Requires-Dist: backcall (>=0.2.0,<0.3.0)
Requires-Dist: beautifulsoup4 (>=4.12.3,<5.0.0)
Requires-Dist: bleach (>=6.1.0,<7.0.0)
Requires-Dist: certifi (>=2023.11.17,<2024.0.0)
Requires-Dist: cffi (>=1.16.0,<2.0.0)
Requires-Dist: charset-normalizer (>=3.3.2,<4.0.0)
Requires-Dist: comm (>=0.2.1,<0.3.0)
Requires-Dist: contourpy (>=1.2.0,<2.0.0)
Requires-Dist: cycler (>=0.12.1,<0.13.0)
Requires-Dist: debugpy (>=1.8.0,<2.0.0)
Requires-Dist: decorator (>=5.1.1,<6.0.0)
Requires-Dist: defusedxml (>=0.7.1,<0.8.0)
Requires-Dist: easyprocess (>=1.1,<2.0)
Requires-Dist: entrypoint2 (>=1.1,<2.0)
Requires-Dist: et-xmlfile (>=1.1.0,<2.0.0)
Requires-Dist: exceptiongroup (>=1.2.0,<2.0.0)
Requires-Dist: executing (>=2.0.1,<3.0.0)
Requires-Dist: fastjsonschema (>=2.19.1,<3.0.0)
Requires-Dist: fonttools (>=4.47.2,<5.0.0)
Requires-Dist: fqdn (>=1.5.1,<2.0.0)
Requires-Dist: frozenlist (>=1.4.1,<2.0.0)
Requires-Dist: h11 (>=0.14.0,<0.15.0)
Requires-Dist: httpcore (>=1.0.2,<2.0.0)
Requires-Dist: httpx (>=0.26.0,<0.27.0)
Requires-Dist: idna (>=3.6,<4.0)
Requires-Dist: ipykernel (>=6.29.0,<7.0.0)
Requires-Dist: ipylab (>=1.0.0,<2.0.0)
Requires-Dist: ipython (>=8.20.0,<9.0.0)
Requires-Dist: ipython-genutils (>=0.2.0,<0.3.0)
Requires-Dist: ipywidgets (>=8.1.1,<9.0.0)
Requires-Dist: isoduration (>=20.11.0,<21.0.0)
Requires-Dist: jedi (>=0.19.1,<0.20.0)
Requires-Dist: jeepney (>=0.8.0,<0.9.0)
Requires-Dist: jinja2 (>=3.1.3,<4.0.0)
Requires-Dist: json-stream (>=2.3.2,<3.0.0)
Requires-Dist: json-stream-rs-tokenizer (>=0.4.25,<0.5.0)
Requires-Dist: json5 (>=0.9.14,<0.10.0)
Requires-Dist: jsonpointer (>=2.4,<3.0)
Requires-Dist: jsonschema (>=4.21.1,<5.0.0)
Requires-Dist: jupyter-app-launcher (>=0.2.1,<0.3.0)
Requires-Dist: jupyter-client (>=8.6.0,<9.0.0)
Requires-Dist: jupyter-contrib-core (>=0.4.2,<0.5.0)
Requires-Dist: jupyter-contrib-nbextensions (>=0.7.0,<0.8.0)
Requires-Dist: jupyter-core (>=5.7.1,<6.0.0)
Requires-Dist: jupyter-events (>=0.9.0,<0.10.0)
Requires-Dist: jupyter-highlight-selected-word (>=0.2.0,<0.3.0)
Requires-Dist: jupyter-lsp (>=2.2.2,<3.0.0)
Requires-Dist: jupyter-nbextensions-configurator (>=0.6.3,<0.7.0)
Requires-Dist: jupyter-server (>=2.12.5,<3.0.0)
Requires-Dist: jupyter-server-proxy (>=4.1.0,<5.0.0)
Requires-Dist: jupyter-server-terminals (>=0.5.2,<0.6.0)
Requires-Dist: jupyterlab (>=4.0.11,<5.0.0)
Requires-Dist: jupyterlab-pygments (>=0.3.0,<0.4.0)
Requires-Dist: jupyterlab-server (>=2.25.2,<3.0.0)
Requires-Dist: jupyterlab-templates (>=0.5.0,<0.6.0)
Requires-Dist: jupyterlab-widgets (>=3.0.9,<4.0.0)
Requires-Dist: kiwisolver (>=1.4.5,<2.0.0)
Requires-Dist: lxml (>=5.1.0,<6.0.0)
Requires-Dist: markupsafe (>=2.1.4,<3.0.0)
Requires-Dist: matplotlib (>=3.8.2,<4.0.0)
Requires-Dist: matplotlib-inline (>=0.1.6,<0.2.0)
Requires-Dist: mistune (>=3.0.2,<4.0.0)
Requires-Dist: mouseinfo (>=0.1.3,<0.2.0)
Requires-Dist: mss (>=9.0.1,<10.0.0)
Requires-Dist: multidict (>=6.0.4,<7.0.0)
Requires-Dist: nbclassic (>=1.0.0,<2.0.0)
Requires-Dist: nbclient (>=0.9.0,<0.10.0)
Requires-Dist: nbconvert (>=7.14.2,<8.0.0)
Requires-Dist: nbformat (>=5.9.2,<6.0.0)
Requires-Dist: nest-asyncio (>=1.6.0,<2.0.0)
Requires-Dist: notebook (>=7.0.7,<8.0.0)
Requires-Dist: notebook-shim (>=0.2.3,<0.3.0)
Requires-Dist: numpy (>=1.26.3,<2.0.0)
Requires-Dist: openpyxl (>=3.1.2,<4.0.0)
Requires-Dist: overrides (>=7.7.0,<8.0.0)
Requires-Dist: packaging (>=23.2,<24.0)
Requires-Dist: pandas (>=2.2.0,<3.0.0)
Requires-Dist: pandocfilters (>=1.5.1,<2.0.0)
Requires-Dist: parso (>=0.8.3,<0.9.0)
Requires-Dist: pexpect (>=4.9.0,<5.0.0)
Requires-Dist: pickleshare (>=0.7.5,<0.8.0)
Requires-Dist: pillow (>=10.2.0,<11.0.0)
Requires-Dist: platformdirs (>=4.1.0,<5.0.0)
Requires-Dist: prometheus-client (>=0.19.0,<0.20.0)
Requires-Dist: prompt-toolkit (>=3.0.43,<4.0.0)
Requires-Dist: psutil (>=5.9.8,<6.0.0)
Requires-Dist: ptyprocess (>=0.7.0,<0.8.0)
Requires-Dist: pure-eval (>=0.2.2,<0.3.0)
Requires-Dist: pyautogui (>=0.9.54,<0.10.0)
Requires-Dist: pycares (>=4.4.0,<5.0.0)
Requires-Dist: pycparser (>=2.21,<3.0)
Requires-Dist: pygetwindow (>=0.0.9,<0.0.10)
Requires-Dist: pygments (>=2.17.2,<3.0.0)
Requires-Dist: pyjwt (>=2.8.0,<3.0.0)
Requires-Dist: pymsgbox (>=1.0.9,<2.0.0)
Requires-Dist: pyparsing (>=3.1.1,<4.0.0)
Requires-Dist: pyperclip (>=1.8.2,<2.0.0)
Requires-Dist: pyrect (>=0.2.0,<0.3.0)
Requires-Dist: pyrsistent (>=0.20.0,<0.21.0)
Requires-Dist: pyscreenshot (>=3.1,<4.0)
Requires-Dist: pyscreeze (>=0.1.30,<0.2.0)
Requires-Dist: python-dateutil (>=2.8.2,<3.0.0)
Requires-Dist: python-dotenv (>=1.0.1,<2.0.0)
Requires-Dist: python-json-logger (>=2.0.7,<3.0.0)
Requires-Dist: python3-xlib (>=0.15,<0.16)
Requires-Dist: pytweening (>=1.0.7,<2.0.0)
Requires-Dist: pytz (>=2023.4,<2024.0)
Requires-Dist: pyyaml (>=6.0.1,<7.0.0)
Requires-Dist: pyzmq (>=25.1.2,<26.0.0)
Requires-Dist: requests (>=2.31.0,<3.0.0)
Requires-Dist: rfc3339-validator (>=0.1.4,<0.2.0)
Requires-Dist: rfc3986-validator (>=0.1.1,<0.2.0)
Requires-Dist: send2trash (>=1.8.2,<2.0.0)
Requires-Dist: simpervisor (>=1.0.0,<2.0.0)
Requires-Dist: six (>=1.16.0,<2.0.0)
Requires-Dist: sniffio (>=1.3.0,<2.0.0)
Requires-Dist: soupsieve (>=2.5,<3.0)
Requires-Dist: stack-data (>=0.6.3,<0.7.0)
Requires-Dist: tenacity (>=8.2.3,<9.0.0)
Requires-Dist: terminado (>=0.18.0,<0.19.0)
Requires-Dist: tinycss2 (>=1.2.1,<2.0.0)
Requires-Dist: tomli (>=2.0.1,<3.0.0)
Requires-Dist: tornado (>=6.4,<7.0)
Requires-Dist: traitlets (>=5.14.1,<6.0.0)
Requires-Dist: typing-extensions (>=4.9.0,<5.0.0)
Requires-Dist: tzdata (>=2023.4,<2024.0)
Requires-Dist: uri-template (>=1.3.0,<2.0.0)
Requires-Dist: urllib3 (>=2.1.0,<3.0.0)
Requires-Dist: wcwidth (>=0.2.13,<0.3.0)
Requires-Dist: webcolors (>=1.13,<2.0)
Requires-Dist: webencodings (>=0.5.1,<0.6.0)
Requires-Dist: websocket-client (>=1.7.0,<2.0.0)
Requires-Dist: widgetsnbextension (>=4.0.9,<5.0.0)
Requires-Dist: yarl (>=1.9.4,<2.0.0)
Project-URL: Repository, https://gitlab.sikt.no/kudaf/jupyterlab
Description-Content-Type: text/markdown

# Kudaf Analytics platform on JupyterLab 

This analytics platform is **intended to be used by a Kudaf Data Consumer**. It provides the basic infrastructure to interact with the Kudaf system, namely to: 
- **OAuth2 Feide authorization and authentication** to receive a JWT (JSON Web Token). 
- This **JWT will allow access to the granted data variables** on the different datasources. 
- Finally, the user will be able to retrieve the granted data using the notebook and perform analytics on it.
 
## On JupyterLab 

From the [JupyterLab Documentation](https://jupyterlab.readthedocs.io/en/latest/):

    JupyterLab is a highly extensible, feature-rich notebook authoring 
    application and editing environment, and is a part of Project Jupyter, 
    a large umbrella project centered around the goal of providing tools 
    (and standards) for interactive computing with computational notebooks.

    A computational notebook is a shareable document that combines computer 
    code, plain language descriptions, data, rich visualizations like 3D 
    models, charts, graphs and figures, and interactive controls. A notebook, 
    along with an editor like JupyterLab, provides a fast interactive 
    environment for prototyping and explaining code, exploring and 
    visualizing data, and sharing ideas with others.

Click on the link for the documentation on how to [Get Started with Jupyterlab](https://jupyterlab.readthedocs.io/en/latest/getting_started/overview.html).

## Local installation instructions (Linux/Mac)  

### Make sure Python3 is installed on your computer (versions from 3.8 up to 3.11 should work fine)

\$ `python3 --version` 

### Navigate to the folder chosen to contain this project

\$ `cd path/to/desired/folder` 

### Create a Python virtual environment and activate it  

\$ `python3 -m venv .venv` 

This created the virtualenv under the hidden folder `.venv`  

Activate it with: 

\$ `source .venv/bin/activate`  

### Install Kudaf Datasource Tools and other required Python packages 

\$ `pip install kudaf_jupyter_server_extension`  


### Enable the KUDAF extension

\$ `jupyter server extension enable kudaf_extension` 


---

## JupyterLab operation

### Launch the JupyterLab instance

Navigate to the project directory and activate the virtual environment (**if not already activated**): 

\$ `source .venv/bin/activate`  

**Start-up the Jupyter server** 

\$ **`jupyter lab`**  

You should see output very much like this: 


    [I 2023-06-16 12:05:45.548 ServerApp] Jupyter Server 2.6.0 is running at:
    [I 2023-06-16 12:05:45.549 ServerApp] http://localhost:8888/lab?token=db15ff4d84eebd80cd45ac1b03921a2f1e13d6de4f874246
    [I 2023-06-16 12:05:45.549 ServerApp]     http://127.0.0.1:8888/lab?token=db15ff4d84eebd80cd45ac1b03921a2f1e13d6de4f874246
    [I 2023-06-16 12:05:45.549 ServerApp] Use Control-C to stop this server and shut down all kernels (twice to skip confirmation).
    [C 2023-06-16 12:05:45.580 ServerApp] 
        
        To access the server, open this file in a browser:
            file:///home/daniel/.local/share/jupyter/runtime/jpserver-28857-open.html
        Or copy and paste one of these URLs:
            http://localhost:8888/lab?token=db15ff4d84eebd80cd45ac1b03921a2f1e13d6de4f874246
            http://127.0.0.1:8888/lab?token=db15ff4d84eebd80cd45ac1b03921a2f1e13d6de4f874246

This **should also automatically open a new Browser window** pointing to that local Jupyter server at **[http://localhost:8888/lab](http://localhost:8888/lab)**. 

### Kudaf pre-requisites to be able to obtain data

The [Kudaf front-end](https://kudaf-frontend-latest-review-wip-sandbo-9d2e64.paas2.uninett.no/) provides the entry-point for a Kudaf user to: 
- Log into Feide. 
- Browse the Catalog to see what data is available. 
- Select the desired Variables: **NOTE: Not all Variables in Kudaf have (at this point) data to be downloaded** 
- Finish the data-shopping by entering the Project details. 

Now this **data request still needs to be Approved**.: 
- This can be achieved using this [Kudaf Core Backend API endpoint](https://kudaf-core.paas2.uninett.no/docs#/Projects/modify_data_request_approval_status_api_v1_projects_data_request__patch), 
- for which an `admin` scope JWT token is required. 

**Only those approved variables that have defined data-download URLs will be made available** to download here. 

Below is a **list of some of the variables for which data is available**:  
- **Sikt datasource**:
    - `Antall Feide-pålogginger per tjeneste og hos en gitt FeideOrg og grunnskole`** (internal Kudaf variable name: FEIDESTATS_LOGINS_FORG_SCHOOL_SP_ACCUM)
    - `Feide organisasjonsnavn` (FEIDE_ORG_NAVN)
    - `Feide-organisasjons organisasjonsnummer` (FEIDE_ORG_BRREG_ORG)
    - `Feide organisasjons-ID knyttet til en gitt Feide-tjenesteleverandør` (FEIDE_SP_FEIDE_ORG)
    - `Tjenesteleverandørsnavn`  (FEIDE_SP_NAVN)
    - `Tjenesteleverandørsbeskrivelse` (FEIDE_SP_BESKRIVELSE)
- **Brreg datasource**:
    - `Organisasjons navn i Enhetsregisteret` (BRREG_ENHETSNAVN)
    - `En organisasjonenhets kommunetilhørighet` (BRREG_ORG_KOMMUNENR)
    - `En organisasjonenhets fylketilhørighet` (BRREG_ORG_FYLKESNR)
- **SSB datasource**:
    - `Kommunesnavn`  (SSB_KOMMUNENAVN)
    - `Fylkesnavn`  (SSB_FYLKESNAVN)
    - `Fylkeskommunenavn`  (SSB_FYLKESKOMMUNENAVN)
    - `En Kommuneenhets fylketilhørighet` (SSB_KOMMUNENR_FYLKESNR) 
- **NSR datasource**:
    - `Skolenivå` (NSR_ORG_LEVEL)
    - `Antall skoler i en organisasjon` (NSR_ORG_SCHOOL_COUNT)
    - `Antall elever i grunnskole` (NSR_ORG_STUDENT_COUNT) 
  
**Once that is done, we can proceed to retrieve the required data**.

### Run a Notebook to obtain statistics from Feide 

On the left-hand side of the browser window, there should be a file tree open showing the various files on the project's directory. 

**Double-click on the file `kudaf.ipynb`**, this will open the Notebook. 

## Notebook Instructions : Downloading data using the Kudaf Panel

1. Go to the first code cell and enter the following code:  
   
```python
from kudaf_extension import kudaf_notebook
kudaf_notebook.run()

```

2. Execute the code cell (`Ctrl-Enter`) to enable the KUDAF extension. 
3. An **orange button `Enable Kudaf`** will appear on the right-side of the **Top Bar** -> **Click** on it
4. A new browser tab will open to prompt for the **Feide Login**
5. After the login is complete, the **tab will become blank -> Close it**
6. **Go back to the JupyterLab tab**, the orange button from before is now green, and shows we're **logged in**
7. A new **orange button `Granted Variables`** appears now on the right-side of the **Top Bar** -> **Click** on it
8. A **new Jupyter tab `Kudaf Panel` will appear** on the bottom half of the screen **(if it appears blank on your browser, change the Zoom level by pressing `Ctrl +` or `Ctrl -`)**
    - This shows different **Accordions with the User's Projects**.
    - Under each Project Accordion, another **Accordion-style display shows the Datasources and the Variables granted**.
    - **Some Variables may display possible parameter-entry fields**, if defined for the variable in question, so the data **query can be further refined**.
    - Click on the **(orange) Download button to the right of a Variable** to download its data file
9.  **Data files will be saved under Jupyter Lab's `downloads` folder** (click on the folder icon on the Jupyter Lab left side panel to toggle display of the available files and folders)


### Notebook Instructions : Downloading data using Python code (and the Kudaf Jupyter Server Extension)

**Required**: 
- **Name of a Kudaf variable that has been authorized** for you
- **URL** where its data can be downloaded from

A **list of the authorized variables** for the logged-in user can be found at: [Granted Variables](http://localhost:8888/kudaf/settings) 

Below is an **example Async function call that will download the data file** to the **'downloads' folder** on the left panel

```python
var_params = {
    "variable": "FEIDE_SP_NAVN",
    "url": "https://kudaf-feide-stats.paas2.uninett.no/api/v1/fixed/FEIDE_SP_NAVN",
}
result = kudaf_notebook.loop.run_until_complete(kudaf_notebook.fetch_granted_data(var_params))
```


**HAPPY ANALYTICS! :)** 

---

---

## Developer installation

### Clone this repository locally  

Open up a Terminal window and enter the following commands:  

\$ `git clone git@gitlab.sikt.no:kudaf/jupyterlab.git` 



