美文网首页
使用streamlit搭建markdown网页

使用streamlit搭建markdown网页

作者: 啊阿伟啊 | 来源:发表于2023-05-20 18:21 被阅读0次

前言

streamlit 是一个可以快速搭建数据应用服务的python第三方库,而且预定义的样式也非常美观,可以用来做数据的展示、模型能力的挖掘和原型应用。

本文基于streamlit提供的markdown文件处理能力,搭建一个markdown的网页。如果你有相关需求,希望本文能帮到你。

一、安装依赖

pip install streamlit

二、创建应用入口

main.py

import streamlit as st

docs_file = "/app/docs.md"

def read_markdown_file(filepath):
    with open(filepath, 'r', encoding='utf-8') as f:
        return f.read()

text = read_markdown_file(docs_file)
st.markdown(text, unsafe_allow_html=True)

三、启动应用

python -m streamlit run main.py --server.port 8000

说明:

  • --server.port 参数:指定访问应用的端口

四、访问服务

在浏览器输入:http://localhost:8000,就可以看到渲染的页面了

五、容器部署

1. Dockerfile

FROM python:3.10

RUN --mount=type=cache,target=/root/.cache/pip \
    pip install streamlit -i https://pypi.tuna.tsinghua.edu.cn/simple/

COPY . /app

WORKDIR /app

EXPOSE 8000

CMD [ "python", "-m", "streamlit", "run", "main.py", "--server.port", "8000" ]

2. 构建镜像

docker build -t streamlit-markdown .

3. 启动容器

docker run -p 8000:8000 --name markdown-app streamlit-markdown

结语

本文针对于特殊需求实现了简单的markdown内容的页面。

对于需要支持多页面的需求,此处有一个解决方案,大家可以查看这个项目:

streamlit 还有很多组件,功能强大,学习和使用的成本都很低,而且🤗(Hugingface)的space功能对streamlit进行了支持,可以方便的进行部署,查看效果,大家快点动手试试吧。

相关文章

网友评论

      本文标题:使用streamlit搭建markdown网页

      本文链接:https://www.haomeiwen.com/subject/azzsjdtx.html