-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapp.py
71 lines (60 loc) · 2.69 KB
/
app.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
from datetime import datetime
from pathlib import Path
from flask import Flask, render_template, request
from saspy import SASsession
sas_output_dir = Path('sas_output')
app = Flask(__name__, static_folder=sas_output_dir)
response_contents = {
'directory': '',
'directory_list': [],
'dataset': '',
'iframe_filename': '',
'error': '',
}
sas = SASsession()
SAS_COMMANDS = {
'run proc contents': 'proc contents order=varnum data=temp_lib.%s; run;',
'run proc freq': 'proc freq nlevels data=temp_lib.%s; tables _CHAR_; run;',
'run proc means': 'proc means maxdec=2 data=temp_lib.%s; run;',
}
@app.route('/', methods=['GET', 'POST'])
def handle_root_request():
global response_contents
sas_output_dir.mkdir(parents=True, exist_ok=True)
if request.method == 'POST':
command = request.form['command'].lower()
if command == 'reset page':
response_contents['directory'] = ''
response_contents['directory_list'] = []
response_contents['dataset'] = ''
response_contents['iframe_filename'] = ''
response_contents['error'] = ''
elif command == 'submit directory':
directory = Path(request.form['directory'])
if directory.exists() and directory != Path('.'):
response_contents['directory'] = directory
response_contents['directory_list'] = list(directory.glob('*.sas7bdat'))
response_contents['error'] = ''
else:
response_contents['directory'] = ''
response_contents['directory_list'] = ''
response_contents['error'] = 'Directory Choice Is Invalid.'
response_contents['dataset'] = ''
response_contents['iframe_filename'] = ''
else:
response_contents['dataset'] = Path(request.form.get('dataset', ''))
dataset_name = response_contents['dataset'].stem
if dataset_name:
sas.saslib('temp_lib', path=str(response_contents['directory']))
sas_output = sas.submit(SAS_COMMANDS[command] % dataset_name)
iframe_filename = f'results-%s.html' % datetime.now().strftime('%Y-%m-%dT%H-%M-%S')
with open(sas_output_dir/iframe_filename, 'w') as fp:
fp.write(sas_output['LST'])
response_contents['iframe_filename'] = iframe_filename
response_contents['error'] = ''
else:
response_contents['iframe_filename'] = ''
response_contents['error'] = 'No Dataset Selected.'
return render_template('index.html', **response_contents)
if __name__ == '__main__':
app.run(port=8000, debug=True)