首页 > 解决方案 > 在开发 FastAPI 服务器时,如何像在 jupyter 笔记本中一样检查每个变量?

问题描述

我刚刚开始使用fastapipython 开发简单的 ASGI 端点。我注意到处理程序函数代码只能在我使用uvicorn. 例如,拿这个代码。

@app.post("/upload_dataset", tags=["DataFrame Loading Utilities"])
async def upload_dataset_to_server(file: UploadFile = File(...),
                                   encoding='utf-8',
                                   delimiter=',',
                                   sheets='Sheet1'):
    '''
        Upload dataset. Add some more details about the parameters
    '''

    raw = await file.read()
    try:
        """
        TODO: XLSX still has some minor issues with saving
        """

        if 'xlsx' in file.filename.split('.')[-1]:
            df = pd.read_excel(io=raw, sheet_name=sheets)
            filename = (file.filename).replace('xlsx', 'csv')
            filepath = ml.folder_path + \
                f'\\datastore\\dataset\\{filename}'

        else:
            content = str(raw, encoding=encoding)
            data = StringIO(content)
            df = pd.read_csv(filepath_or_buffer=data,
                             encoding=encoding, delimiter=delimiter)
            filepath = ml.folder_path + \
                f'\\datastore\\dataset\\{file.filename}'
            df.to_csv(path_or_buf=filepath, index_label=False)

        resp = ml.store_dataframe(df)
        return resp

    except UnicodeDecodeError as error:
        raise HTTPException(
            status_code=404,
            detail=str(error)
        )

有没有办法像在 jupyter notebook 中一样在函数中逐行运行,以熟悉每个对象代表的内容以及它们提供的接口?在我看来,如果不运行 uvicorn 模块并在服务器运行时打印日志,就无法调用函数或实际检查变量。

标签: pythonjupyter-notebookfastapiasgi

解决方案


推荐阅读