# Running a JupyterLab Notebook

## Use Cases for a Single JupyterLab Instance&#x20;

* R needs to run in a regular notebook and for downstream analysis
* If directly interacting with the database/ dataset, it is recommended that you either 1) use Python and/ or 2) use Spark for extracting the data that is relevant for the downstream analysis&#x20;

## General “Recipe” for Utilizing Single Instance JupyterLab Notebooks&#x20;

1. Create a DX JupyterLab Notebook so that it will automatically save onto the Trusted Research Environment. You can do so by selecting these 2 different options:

   1. Option 1 is from the Launcher:&#x20;

   <figure><img src="https://1979569080-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FPtCOm9rXoRi4P9rh1ET8%2Fuploads%2FKGeltYq0mQ8qszhf0oCW%2Funknown.png?alt=media&#x26;token=c15f6977-fff9-4ada-ac84-8972ae785ff9" alt=""><figcaption></figcaption></figure>

   b.  Option 2 is from the DNAnexus Tab:

<figure><img src="https://1979569080-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FPtCOm9rXoRi4P9rh1ET8%2Fuploads%2FrigDukVS1is6JmgTmRSV%2Funknown.png?alt=media&#x26;token=e130df61-03d5-4a68-880d-8a5856f0dcb4" alt=""><figcaption></figcaption></figure>

2. Start writing your JupyterLab Notebook. Select which kernel you are going to use (options will vary depending on the Image you selected in set up).&#x20;
3. Download packages and save the software environment as a snapshot&#x20;

   1. Download Packages&#x20;

   ```
   pip install ___ #python
   install.packages() #R
   ```

   b.  Save the Snapshot of the environment&#x20;
4. Start writing your code.&#x20;

   1. Load Packages&#x20;

   <pre><code><strong>import ____ #python
   </strong>library() #R
   </code></pre>

   &#x20; b.  Download or Access data files to the JupyterLab environment

   ```
   %%bash 
   #option 1: dx download 
   dx download "PATH TO FILE"

   #option 2: dx fuse 
   data = pd.read_csv("/mnt/project/PATH.csv")
   ```

   &#x20; c.  Import the data

   ```
   import ___ as pd 
   NAME = pd.read_csv("PATH.csv")
   ```

   &#x20; d.  Then, perform the analysis for your data

   &#x20; e. Upload results back to Project Space

   ```
   %%bash 
   dx upload FILE --destination /your/path/for/results
   ```
5. Save your DX Jupyterlab Notebook&#x20;

## Opening Notebooks from Project Storage

* Notebooks can also be directly opened from project storage

<figure><img src="https://1979569080-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FPtCOm9rXoRi4P9rh1ET8%2Fuploads%2FVCqOPDA4haaDnCax8YKb%2Funknown.png?alt=media&#x26;token=004aa09b-b8ca-4ce6-b3bf-fcb26dae1b33" alt=""><figcaption></figcaption></figure>

* When you save in JupyterLab, the notebook gets uploaded to the platform as a new file. This goes back to the concept of immutability.
* The old version of notebook goes into .Notebook\_archive/ folder in project.
