@@ -33,28 +33,31 @@ def create_app(script_info: Optional[ScriptInfo] = None, data_path: Text = "data
33
33
34
34
cell_image_cache : Dict [Tuple [Text , int ], CellGrid [PIL .Image ]] = {}
35
35
36
- def get_workdir (project : Text , subdir : Text ) -> Text :
37
- return os .path .join (app .config [DATA_PATH ], project , subdir )
38
-
39
- @api .route ('/' )
40
- def get_all_projects ():
41
- project_names = [os .path .join (app .config [DATA_PATH ], project_name )
42
- for project_name in os .listdir (app .config [DATA_PATH ])]
43
- project_names = [pn for pn in project_names if os .path .isdir (pn )]
44
- project_names = [pn for pn in project_names if not pn .startswith ('.' )]
45
- project_names = [os .path .basename (pn ) for pn in project_names ]
36
+ def get_workdir (bucket : Text , project : Text , subdir : Text ) -> Text :
37
+ return os .path .join (app .config [DATA_PATH ], bucket , project , subdir )
38
+
39
+ @api .route ("/" )
40
+ def get_all_status_folders ():
41
+ data_path = app .config [DATA_PATH ]
42
+ project_buckets = table_annotator .io .get_all_non_hidden_dirs (data_path ,
43
+ return_base_names = True )
44
+ return {"bucketNames" : sorted (project_buckets )}
45
+
46
+ @api .route ('/<bucket>/' )
47
+ def get_all_projects (bucket : str ):
48
+ bucket_dir = os .path .join (app .config [DATA_PATH ], bucket )
49
+ if not os .path .isdir (bucket_dir ):
50
+ return make_response ({"msg" : "The state dir does not exist." }, 404 )
51
+ project_names = table_annotator .io .get_all_non_hidden_dirs (bucket_dir ,
52
+ return_base_names = True )
46
53
return {"projectNames" : sorted (project_names )}
47
54
48
- @api .route ('/<project>' )
49
- def get_project (project : Text ):
50
- project_path = os .path .join (app .config [DATA_PATH ], project )
55
+ @api .route ('/<bucket>/< project>' )
56
+ def get_project (bucket : str , project : str ):
57
+ project_path = os .path .join (app .config [DATA_PATH ], bucket , project )
51
58
if not os .path .isdir (project_path ):
52
59
return make_response ({"msg" : "The project does not exist." }, 404 )
53
-
54
- work_dirs = [os .path .join (os .path .join (project_path , d ))
55
- for d in os .listdir (project_path )]
56
- work_dirs = [d for d in work_dirs if os .path .isdir (d )]
57
-
60
+ work_dirs = table_annotator .io .get_all_non_hidden_dirs (project_path )
58
61
work_dir_infos = []
59
62
60
63
for work_dir in work_dirs :
@@ -86,9 +89,9 @@ def get_project(project: Text):
86
89
"workPackages" : work_dir_infos
87
90
}}
88
91
89
- @api .route ('/<project>/<subdir>/images' )
90
- def list_images (project : Text , subdir : Text ):
91
- workdir = get_workdir (project , subdir )
92
+ @api .route ('/<bucket>/< project>/<subdir>/images' )
93
+ def list_images (bucket : Text , project : Text , subdir : Text ):
94
+ workdir = get_workdir (bucket , project , subdir )
92
95
if not os .path .isdir (workdir ):
93
96
return make_response (
94
97
{"msg" : "The workdir you tried to access does not exist." }, 404 )
@@ -105,17 +108,17 @@ def list_images(project: Text, subdir: Text):
105
108
)
106
109
center = {"x" : width // 2 , "y" : height // 2 }
107
110
images_with_metadata .append (
108
- {"src" : f"{ project } /{ subdir } /image/{ image_name } " , "width" : width ,
111
+ {"src" : f"{ bucket } / { project } /{ subdir } /image/{ image_name } " , "width" : width ,
109
112
"height" : height , "center" : center ,
110
113
"name" : image_name , "docId" : os .path .splitext (image_name )[0 ],
111
114
"hasPreAnnotatedData" : has_pre_annotated_data ,
112
115
"hasMatchingData" : has_matching_data
113
116
})
114
117
return {"images" : images_with_metadata }
115
118
116
- @api .route ('/<project>/<subdir>/image/invert/<image_name>' , methods = ["POST" ])
117
- def invert_image (project : Text , subdir : Text , image_name : Text ):
118
- workdir = get_workdir (project , subdir )
119
+ @api .route ('/<bucket>/< project>/<subdir>/image/invert/<image_name>' , methods = ["POST" ])
120
+ def invert_image (bucket : Text , project : Text , subdir : Text , image_name : Text ):
121
+ workdir = get_workdir (bucket , project , subdir )
119
122
image_path = os .path .join (workdir , image_name )
120
123
if not os .path .isfile (image_path ):
121
124
return make_response ({"msg" : "The image you tried to invert"
@@ -125,9 +128,9 @@ def invert_image(project: Text, subdir: Text, image_name: Text):
125
128
table_annotator .io .write_image (image_path , image )
126
129
return make_response ({"msg" : "Ok" }, 200 )
127
130
128
- @api .route ('/<project>/<subdir>/image/rotate/<image_name>' , methods = ["POST" ])
129
- def rotate_image (project : Text , subdir : Text , image_name : Text ):
130
- workdir = get_workdir (project , subdir )
131
+ @api .route ('/<bucket>/< project>/<subdir>/image/rotate/<image_name>' , methods = ["POST" ])
132
+ def rotate_image (bucket : Text , project : Text , subdir : Text , image_name : Text ):
133
+ workdir = get_workdir (bucket , project , subdir )
131
134
image_path = os .path .join (workdir , image_name )
132
135
if not os .path .isfile (image_path ):
133
136
return make_response ({"msg" : "The image you tried to invert"
@@ -137,9 +140,9 @@ def rotate_image(project: Text, subdir: Text, image_name: Text):
137
140
table_annotator .io .write_image (image_path , image )
138
141
return make_response ({"msg" : "Ok" }, 200 )
139
142
140
- @api .route ('/<project>/<subdir>/image/<image_name>' )
141
- def get_image (project : Text , subdir : Text , image_name : Text ):
142
- workdir = get_workdir (project , subdir )
143
+ @api .route ('/<bucket>/< project>/<subdir>/image/<image_name>' )
144
+ def get_image (bucket : Text , project : Text , subdir : Text , image_name : Text ):
145
+ workdir = get_workdir (bucket , project , subdir )
143
146
return send_from_directory (workdir , image_name )
144
147
145
148
@api .route ('/<project>/<subdir>/state/<image_name>' , methods = ["GET" ])
@@ -153,9 +156,9 @@ def get_document_state(project: Text, subdir: Text, image_name: Text):
153
156
state = table_annotator .io .read_state_for_image (image_path )
154
157
return state .dict ()
155
158
156
- @api .route ('/<project>/<subdir>/state/<image_name>' , methods = ["POST" ])
157
- def set_document_state (project : Text , subdir : Text , image_name : Text ):
158
- workdir = get_workdir (project , subdir )
159
+ @api .route ('/<bucket>/< project>/<subdir>/state/<image_name>' , methods = ["POST" ])
160
+ def set_document_state (bucket : Text , project : Text , subdir : Text , image_name : Text ):
161
+ workdir = get_workdir (bucket , project , subdir )
159
162
image_path = os .path .join (workdir , image_name )
160
163
if not os .path .isfile (image_path ):
161
164
return make_response ({"msg" : "The image for which you tried to save "
@@ -166,10 +169,10 @@ def set_document_state(project: Text, subdir: Text, image_name: Text):
166
169
167
170
return {"msg" : "OK" }
168
171
169
- @api .route ('/<project>/<subdir>/tables/<image_name>' , methods = ["POST" ])
170
- def store_tables (project : Text , subdir : Text , image_name : Text ):
172
+ @api .route ('/<bucket>/< project>/<subdir>/tables/<image_name>' , methods = ["POST" ])
173
+ def store_tables (bucket : Text , project : Text , subdir : Text , image_name : Text ):
171
174
"""Stores the tables."""
172
- workdir = get_workdir (project , subdir )
175
+ workdir = get_workdir (bucket , project , subdir )
173
176
image_path = os .path .join (workdir , image_name )
174
177
if not os .path .isfile (image_path ):
175
178
return make_response ({"msg" : "The image for which you tried to save "
@@ -180,9 +183,9 @@ def store_tables(project: Text, subdir: Text, image_name: Text):
180
183
181
184
return {"msg" : "OK" }
182
185
183
- @api .route ('/<project>/<subdir>/tables/<image_name>' , methods = ["GET" ])
184
- def get_tables (project : Text , subdir : Text , image_name : Text ):
185
- workdir = get_workdir (project , subdir )
186
+ @api .route ('/<bucket>/< project>/<subdir>/tables/<image_name>' , methods = ["GET" ])
187
+ def get_tables (bucket , project : Text , subdir : Text , image_name : Text ):
188
+ workdir = get_workdir (bucket , project , subdir )
186
189
image_path = os .path .join (workdir , image_name )
187
190
if not os .path .isfile (image_path ):
188
191
return make_response ({"msg" : "The image for which you tried to retrieve "
@@ -196,11 +199,11 @@ def get_tables(project: Text, subdir: Text, image_name: Text):
196
199
return tables_json
197
200
198
201
@api .route (
199
- '/<project>/<subdir>/<image_name>/predict_table_structure/<int:table_id>' ,
202
+ '/<bucket>/< project>/<subdir>/<image_name>/predict_table_structure/<int:table_id>' ,
200
203
methods = ["GET" ])
201
- def predict_table_structure (project : Text , subdir : Text , image_name : Text ,
204
+ def predict_table_structure (bucket : Text , project : Text , subdir : Text , image_name : Text ,
202
205
table_id : int ):
203
- workdir = get_workdir (project , subdir )
206
+ workdir = get_workdir (bucket , project , subdir )
204
207
image_path = os .path .join (workdir , image_name )
205
208
if not os .path .isfile (image_path ):
206
209
return make_response ({"msg" : "The image does not exist." }, 404 )
@@ -221,11 +224,11 @@ def predict_table_structure(project: Text, subdir: Text, image_name: Text,
221
224
222
225
return {"rows" : rows }
223
226
224
- @api .route ('/<project>/<subdir>/<image_name>/predict_table_contents/<int:table_id>' ,
227
+ @api .route ('/<bucket>/< project>/<subdir>/<image_name>/predict_table_contents/<int:table_id>' ,
225
228
methods = ["GET" ])
226
- def predict_table_contents (project : Text , subdir : Text ,
229
+ def predict_table_contents (bucket : Text , project : Text , subdir : Text ,
227
230
image_name : Text , table_id : int ):
228
- workdir = get_workdir (project , subdir )
231
+ workdir = get_workdir (bucket , project , subdir )
229
232
image_path = os .path .join (workdir , image_name )
230
233
231
234
if not os .path .isfile (image_path ):
@@ -255,11 +258,11 @@ def predict_table_contents(project: Text, subdir: Text,
255
258
updated_cells ),
256
259
"columnTypes" : column_types }
257
260
258
- @api .route ('/<project>/<subdir>/<image_name>/match_table_contents/<int:table_id>' ,
261
+ @api .route ('/<bucket>/< project>/<subdir>/<image_name>/match_table_contents/<int:table_id>' ,
259
262
methods = ["GET" ])
260
- def match_table_contents (project : Text , subdir : Text ,
263
+ def match_table_contents (bucket : Text , project : Text , subdir : Text ,
261
264
image_name : Text , table_id : int ):
262
- workdir = get_workdir (project , subdir )
265
+ workdir = get_workdir (bucket , project , subdir )
263
266
image_path = os .path .join (workdir , image_name )
264
267
265
268
if not os .path .isfile (image_path ):
@@ -285,11 +288,11 @@ def match_table_contents(project: Text, subdir: Text,
285
288
286
289
287
290
@api .route (
288
- '/<project>/<subdir>/<image_name>/apply_pre_annotated_table_content/<int:table_id>' ,
291
+ '/<bucket>/< project>/<subdir>/<image_name>/apply_pre_annotated_table_content/<int:table_id>' ,
289
292
methods = ["GET" ])
290
- def apply_pre_annotated_table_content (project : Text , subdir : Text ,
293
+ def apply_pre_annotated_table_content (bucket : Text , project : Text , subdir : Text ,
291
294
image_name : Text , table_id : int ):
292
- workdir = get_workdir (project , subdir )
295
+ workdir = get_workdir (bucket , project , subdir )
293
296
image_path = os .path .join (workdir , image_name )
294
297
295
298
if not os .path .isfile (image_path ):
@@ -313,12 +316,12 @@ def apply_pre_annotated_table_content(project: Text, subdir: Text,
313
316
lambda c : {k : v for k , v in c .dict ().items () if v is not None },
314
317
updated_cells )}
315
318
316
- @api .route ('/<project>/<subdir>/<image_name>/cell_image/<int:table_id>/<int:row>/'
319
+ @api .route ('/<bucket>/< project>/<subdir>/<image_name>/cell_image/<int:table_id>/<int:row>/'
317
320
'<int:col>/<int:table_hash>' ,
318
321
methods = ["GET" ])
319
- def get_cell_image (project : Text , subdir : Text , image_name : Text ,
322
+ def get_cell_image (bucket : Text , project : Text , subdir : Text , image_name : Text ,
320
323
table_id : int , row : int , col : int , table_hash : int ):
321
- workdir = get_workdir (project , subdir )
324
+ workdir = get_workdir (bucket , project , subdir )
322
325
image_path = os .path .join (workdir , image_name )
323
326
if (image_path , table_hash ) not in cell_image_cache :
324
327
if not os .path .isfile (image_path ):
0 commit comments