Fastapi: How to import markdown files

Created on 10 Dec 2020  路  4Comments  路  Source: tiangolo/fastapi

What is the best way to import a markdown file?
I have markdown files in the app/pages/ directory and want to import them.

I'd like to use these markdown files for frontend texts with using Python-Markdown.

I tried this but didn't work:

readme_file = open("./pages/about.md", "r")
md_template_string = markdown.markdown(
        readme_file.read()
    )
question

All 4 comments

If this is related to FastAPI, you probably need to give more context here...

Is this useful? https://python-markdown.github.io/reference/#markdown

I used it in this way:

@app.get("/markdown", response_class=HTMLResponse)
async def color_home(request: Request):
    with open("./pages/about.md", "r", encoding="utf-8") as input_file:
        text = input_file.read()
    html = markdown.markdown(text)

    data = {
        "title": "About Artist Tools",
        "text": html
    }

    return templates.TemplateResponse("page.html", {"request": request, "data": data})

And I get the following error:

with open("./pages/about.md", "r", encoding="utf-8") as input_file:
FileNotFoundError: [Errno 2] No such file or directory: './pages/about.md'

@shinokada well this is j谋st a FileNotFoundError.

Change the path to app/pages/about.md I'm not sure though but you need to change the path.

@ycd
Thank it worked.

Was this page helpful?
0 / 5 - 0 ratings