-
Notifications
You must be signed in to change notification settings - Fork 1
/
api.py
46 lines (38 loc) · 1.36 KB
/
api.py
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
35
36
37
38
39
40
41
42
43
44
from flask import Flask
import importlib
import pandas as pd
import yaml
import json
import subprocess
import re
app = Flask(__name__)
@app.route('/pkg_versions')
def pkg_versions():
with open('environment.yml') as f:
environment = yaml.load(f)
versions = {}
non_packages = {}
for pkg_name in environment['dependencies']:
pkg_name = re.sub('[=<>][^\n]+', '',pkg_name)
try:
pkg = importlib.import_module(pkg_name)
versions[pkg_name] = pkg.__version__
except Exception as e:
try:
non_packages[pkg_name] = subprocess.check_output([pkg_name,'--version']).decode('utf-8')
except Exception as e:
try:
non_packages[pkg_name] = subprocess.check_output([pkg_name,'version']).decode('utf-8')
except Exception as e:
non_packages[pkg_name] = str(e)
return json.dumps({'package_versions':versions, 'non-python-packages':non_packages})
@app.route('/test_pandas')
def test_pandas():
df = pd.DataFrame({'a':[1,2,3,4],'b':[9,10,0,0]})
answer = {'df':df.to_dict(),
'a_sum': df['a'].sum(),
'a+b':(df['a']+df['b']).tolist(),
'sum of a grouped by b': df.groupby('b')['a'].sum().to_dict()}
return json.dumps(answer)
if __name__ == '__main__':
app.run(debug = True)