Open ripry opened 1 year ago
Workaround is here:
from tempfile import NamedTemporaryFile
from fastapi import APIRouter, UploadFile
from rdflib import Graph
router = APIRouter()
@router.post('/parse')
async def parse(file: UploadFile):
with NamedTemporaryFile() as tmp:
with open(tmp.name, "w+b") as writer:
for chunk in file.file:
writer.write(chunk)
with open(tmp.name, "r+b") as reader:
graph = Graph().parse(file=reader)
Thanks for reporting things @ripry, it does seem like something is wrong here.
Could you try this though:
from fastapi import APIRouter, UploadFile
from rdflib import Graph
router = APIRouter()
@router.post('/parse')
async def parse(file: UploadFile):
graph = Graph().parse(source=file.file)
I think it should work, but not sure. Either way, I think this is a bug.
@aucampia Thanks for the reply!
I have already tried Graph().parse(source=file.file)
.
The following error occurs:
File "path/to/source/app.py", line 6, in parse
graph = Graph().parse(source=file.file)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/rdflib/graph.py", line 1494, in parse
parser.parse(source, self, **args)
File "/usr/local/lib/python3.11/site-packages/rdflib/plugins/parsers/notation3.py", line 2015, in parse
baseURI = graph.absolutize(source.getPublicId() or source.getSystemId() or "")
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/rdflib/graph.py", line 1225, in absolutize
return self.namespace_manager.absolutize(uri, defrag)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/rdflib/namespace/__init__.py", line 722, in absolutize
result = urljoin("%s/" % base, uri, allow_fragments=not defrag)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/urllib/parse.py", line 521, in urljoin
base, url, _coerce_result = _coerce_args(base, url)
^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/urllib/parse.py", line 121, in _coerce_args
raise TypeError("Cannot mix str and non-str arguments")
TypeError: Cannot mix str and non-str arguments
This error occurs when the uploaded file is larger than the max_size of SpooledTemporaryFile. (The default of max_size is 1 MB in FastAPI.)
If it is correct to use Graph().parse(source=file.file)
, we need another issue...
Hi, there! I'm using RDFLib with FastAPI. The following code passed the file received by FastAPI's UploadFile to RDFLib:
Then I got the following error:
I think it is because FastAPI uses SpooledTemporaryFile in UploadFile entity and the name can be an int. (ref: https://github.com/python/cpython/issues/62095) So we need to modify the part where the
system_id = URIRef(pathlib.Path(file.name).absolute().as_uri(), base=base)
file path is created. But I can't think of a good idea...