瀏覽代碼

Add example to get all cells

Arkadiusz Ryś 1 年之前
父節點
當前提交
4ba50a3a8d
共有 3 個文件被更改,包括 29 次插入4 次删除
  1. 2 2
      .gitignore
  2. 25 0
      spendpoint/router.py
  3. 2 2
      tasks.py

+ 2 - 2
.gitignore

@@ -8,5 +8,5 @@ dist/
 .idea
 
 # Generated example files
-data/example.csv
-data/example.parquet
+data/example*.csv
+data/example*.parquet

+ 25 - 0
spendpoint/router.py

@@ -136,6 +136,31 @@ class SparqlRouter(APIRouter):
             return await sparql_endpoint_get(request, query)
 
 
+        @self.get("/cell/{file_name}/")
+        async def sparql_sheet_endpoint_get(request: Request, file_name, query: Optional[str] = Query(None)) -> Response:
+            logging.debug(f"Received sheet GET request [{file_name}].")
+            graph = ConjunctiveGraph()
+            graph_ns = dict(graph.namespaces())
+            # graph_ns["tabular"] = "http://ua.be/sdo2l/vocabulary/formalisms/tabular#"
+            df = pd.read_csv(f"data/{file_name}", index_col=None, header=None)
+            df.reset_index()
+
+            # Please forgive me pandas gods
+            for i in range(df.shape[0]):
+                for j in range(df.shape[1]):
+                    cell_value = df.at[i, j]
+                    cell = URIRef(f"http://ua.be/sdo2l/vocabulary/formalisms/tabular#Cell_{i}_{j}")
+                    graph.add((cell, URIRef("http://ua.be/sdo2l/vocabulary/formalisms/tabular#holdsContent"), Literal(cell_value)))
+                    graph.add((cell, URIRef("http://ua.be/sdo2l/vocabulary/formalisms/tabular#hasRowPosition"), Literal(int(i))))
+                    graph.add((cell, URIRef("http://ua.be/sdo2l/vocabulary/formalisms/tabular#hasColumnPosition"), Literal(int(j))))
+
+            try:
+                query_results = graph.query(query, initNs=graph_ns)
+            except Exception as e:
+                logging.error("Error executing the SPARQL query on the RDFLib Graph: " + str(e))
+                return JSONResponse(status_code=400, content={"message": "Error executing the SPARQL query on the RDFLib Graph."})
+            return await encode_graph_query_results(request, query_results)
+
         @self.get("/cell/{file_name}/{row}/{column}/")
         async def sparql_cell_endpoint_get(request: Request, file_name, row, column, query: Optional[str] = Query(None)) -> Response:
             """

+ 2 - 2
tasks.py

@@ -74,12 +74,12 @@ def release(c, version):
 
 
 @task(name="generate", aliases=("gen", "csv"))
-def generate_random_data_csv(c, rows=200000, columns=50):
+def generate_random_data_csv(c, rows=200000, columns=50, name="example"):
     """"""
     import numpy as np
     import uuid
     data_dir = Path(__file__).resolve().parent / Path("data")
-    out_file_path = data_dir / Path("example.csv")
+    out_file_path = data_dir / Path(f"{name}.csv")
     chunk = 1000
     current_row = 0
     with out_file_path.open("w", encoding="utf-8", buffering=chunk) as csv_file: