File size: 1,532 Bytes
30ffb9e
 
 
 
 
 
 
19a1609
30ffb9e
19a1609
30ffb9e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import json
import urllib.request

def load_impact_theory_data():
    '''
    Loads impact_theory_data.json data by trying three options:
    1. Assumes user is in Google Colab environment and loads file from content dir.
    2. If 1st option doesn't work, assumes user is in repo and loads from data dir.
    3. If 2nd option doesn't work, assumes user does not have direct access to data so
       downloads data direct from repo.
    '''
    try:
        path = '/content/impact_theory_data.json'
        with open(path) as f:
            data = json.load(f)
        return data
    except Exception:
        print(f"Data not available at {path}")
        try: 
            path = './data/impact_theory_data.json'
            with open(path) as f:
                data = json.load(f)
            print(f'OK, data available at {path}')
            return data
        except Exception:
            print(f'Data not available at {path}, downloading from source')
            try:
                with urllib.request.urlopen("https://ra.githubusercontent.com/americanthinker/vectorsearch-applications/main/data/impact_theory_data.json") as url:
                    data = json.load(url)
                return data
            except Exception:
                print('Data cannot be loaded from source, please move data file to one of these paths to run this test:\n\
    1. "/content/impact_theory_data.json"   --> if you are in Google Colab\n\
    2. "./data/impact_theory_data.json"      --> if you are in a local environment\n')