Skip to content

Commit d05e53c

Browse files
author
rodriguez-facundo
committed
Polish upload/download
1 parent 42b36c0 commit d05e53c

1 file changed

Lines changed: 29 additions & 3 deletions

File tree

netpyne_ui/api.py

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
import os
22
import logging
33
import uuid
4+
import gzip
45
import tarfile
5-
from tempfile import TemporaryDirectory
6+
import shutil
67
from zipfile import ZipFile
8+
from tempfile import TemporaryDirectory
79
from jupyter_geppetto.webapi import get, post
810
from notebook.base.handlers import IPythonHandler
911

10-
ALLOWED_EXTENSIONS = ["py", "zip", "pdf", "txt", "xls", "png", "jpeg"]
12+
ALLOWED_EXTENSIONS = ["py", "zip", "gz", ".tar.gz", "pdf", "txt", "xls", "png", "jpeg"]
1113

1214
UPLOAD_FOLDER_NAME = "uploads"
1315
UPLOAD_FOLDER_PATH = os.path.join(os.getcwd(), UPLOAD_FOLDER_NAME)
@@ -75,6 +77,14 @@ def uploads(handler: IPythonHandler):
7577
if filename.endswith('.zip'):
7678
with ZipFile(file_path) as zipObj:
7779
zipObj.extractall(UPLOAD_FOLDER_PATH)
80+
81+
elif filename.endswith('.tar.gz'):
82+
with tarfile.open(file_path, mode='r:gz') as tar:
83+
tar.extractall(UPLOAD_FOLDER_PATH)
84+
85+
elif filename.endswith('.gz'):
86+
with gzip.open(file_path, "rb") as gz, open(file_path.replace('.gz', ''), 'wb') as ff:
87+
shutil.copyfileobj(gz, ff)
7888

7989
handler.set_status(200, f"Number of files saved: {files_saved}. Number of files sent: {len(files['file'])}")
8090

@@ -105,4 +115,20 @@ def downloads(handler: IPythonHandler):
105115

106116
send_files(handler, tar_gz_file_path, tar_gz_file_name)
107117

108-
handler.finish()
118+
handler.finish()
119+
120+
@get('/export')
121+
def exportModel(self, args):
122+
try:
123+
with redirect_stdout(sys.__stdout__):
124+
if not args['netCells']:
125+
sim.initialize(netParams = self.netParams, simConfig = self.simConfig)
126+
sim.cfg.filename = args['fileName']
127+
include = [el for el in specs.SimConfig().saveDataInclude if el in args.keys() and args[el]]
128+
if args['netCells']: include += ['netPops']
129+
sim.cfg.saveJson = True
130+
sim.saveData(include)
131+
sim.cfg.saveJson = False
132+
return utils.getJSONReply()
133+
except:
134+
return utils.getJSONError("Error while exporting the NetPyNE model", sys.exc_info())

0 commit comments

Comments
 (0)