---
# System prepended metadata

title: Execute API of DAS analysis project Job with environment variables

---

# Execute API of DAS analysis project Job with environment variables


## 目標
* 希望每次都可以輸入參數給 Notebook/refinery Job
* 希望可透過API的方式執行 Notebook/refinery Job


## Create Notebook
* 得到輸入參數

```python
import os
myenv1 = os.getenv('MYENV1', default='no MYENV1')
myenv2 = os.getenv('MYENV2', default='no MYENV2')
myenv3 = os.getenv('MYENV3', default='no MYENV3')

print(myenv1)
print(myenv2)
print(myenv3)
# -output-
# no MYENV1
# no MYENV2
# no MYENV3
```

* 將輸入參數轉換成dataframe格式
```python
import pandas as pd
df = pd.DataFrame({'myenv1' : [myenv1], 'myenv2' : [myenv2], 'myenv3' : [myenv3]})
df
# -output-
#	myenv1	myenv2	myenv3
# 	no MYENV1	no MYENV2	no MYENV3
```

* 將dataframe輸出為一csv檔案, 並加入timestamp作為檔名
```python
from project_lib import Project
project = Project.access()
import datetime
now = datetime.datetime.now(datetime.timezone(datetime.timedelta(hours=9))).strftime('%Y%m%d_%H%M%S')

project.save_data("jov_env_test_"+now+".csv", df.to_csv(),overwrite=True)
```


## Create Job
* create job
![](https://i.imgur.com/IAQaxxt.png)

* 建立名稱
![](https://i.imgur.com/Mju5RwM.png)

* 輸入參數
![](https://i.imgur.com/ARrWk5I.png)


## Run job

* Run
![](https://i.imgur.com/FA0MPGm.png)


* 執行完成
![](https://i.imgur.com/q5l2ZtB.png)


* 下次可更新參數
![](https://i.imgur.com/Cl57n8Y.png)


## 執行結果

* 執行結果，位於data asset
![](https://i.imgur.com/AmBnMd5.png)


* 本範例將input放入欄位中
![](https://i.imgur.com/1yJo2HK.png)


## 外部使用API來執行此Job

### 必須先在project中取得project ID 以及個人的token

```python
import os
os.environ['PROJECT_ID']
token = os.environ['USER_ACCESS_TOKEN']
```

## 前置設定
* Authentication
```python
import requests
url='https://startup.das.twcc.ai'
project_id = '<your project ID>'
token='<your token>'
headers = {
    'Authorization': 'Bearer ' + token,
    'Content-Type': 'application/json'
}



```

* 取得job ID

```python
response = requests.get(url+"/v2/jobs?project_id="+project_id, headers=headers, verify=False).json()
response
```
 
{'total_rows': 1,
 'results': [{'metadata': {'name': 'env_var_input',
    'description': '',
    'asset_id': <font color="#f00">'e72910a2-6489-4776-9e2c-0292f442c865'</font>,
    'owner_id': '1000331007',
    'version': 0},
   'entity': {'job': {'asset_ref_type': 'notebook',
     'configuration': {'env_id': 'jupconda221-48c31f29-0ffe-482f-b863-8914e4e3c4ab',
      'file_ref': '',
      'entrypoint': '',
      'run_results': 'log_and_new_notebook',
      'env_type': 'notebook',
      'env_variables': ['MYENV1=I', 'MYENV2=love', 'MYENV3=NCHC']},
     'last_run_initiator': 'None',
     'last_run_time': '',
     'last_run_status': 'None',
     'last_run_status_timestamp': 0,
     'future_scheduled_runs': [],
     'enable_notifications': True,
     'project_name': 'A+DAS',
     'asset_ref': 'db54743a-093e-435d-bce3-05d307d1b303'}}}]}
     
     
## 透過外部執行Job

* 設定job ID

```python
job_run_id = response['metadata']['asset_id']
job_run_id
```

* 輸入參數設定
```python
jobrunpost = {
  "job_run": {
      "configuration" : {
          "env_variables" :  ["MYENV1=ARGS","MYENV2=from","MYENV3=TWCC"] 
      }
  }
}
```

* call API
```python
response = requests.post(url+"/v2/jobs/"+job_id+"/runs?project_id="+project_id, headers=headers, json=jobrunpost, verify=False).json()
```

## 執行結果

* 執行完成，可在Job run detail看到log

![](https://i.imgur.com/9Ij4uoh.png)



* 最後也有產生一個檔案，內容為剛剛輸入的資料

![](https://i.imgur.com/SDhR84z.png)
