Streamlit使用记录

富文本编辑器streamlit-quill

pip intsall streamlit-quill

调整宽度:

在这里插入图片描述

st.set_page_config(page_title="xxx", layout="wide")
col_main, col_sidebar = st.columns([1, 1]) # 列表的两数代表左右列的宽度

在这里插入图片描述

import streamlit as st
from streamlit_quill import st_quill
import html2markdown
import base64
import os

# 设置页面配置
st.set_page_config(page_title="Markdown 编辑器", layout="wide")

# 初始化会话状态
if "article_content" not in st.session_state:
    st.session_state.article_content = ""

def html_to_markdown(html_content):
    """将HTML内容转换为Markdown格式"""
    return html2markdown.convert(html_content)

def upload_image(image_file):
    """上传图片并返回图片的URL"""
    if image_file is not None:
        # 将图片文件保存到本地
        image_path = os.path.join("uploads", image_file.name)
        with open(image_path, "wb") as f:
            f.write(image_file.getbuffer())
        
        # 返回图片的URL
        return image_path
    return None

# 创建左右两列,设置宽度比例
col1, col2 = st.columns([2, 2])  # 左侧列宽度为2,右侧列宽度为3

# 左侧编辑器
with col1:
    st.header("Markdown 编辑器")
    content = st_quill(
        value=st.session_state.article_content,
        placeholder="开始编写文章...",
        #html=True,# 启用HTML模式
        key="quill_editor",
        # toolbar=[
        #     ['bold', 'italic', 'underline', 'strike'],  # 文字格式
        #     ['blockquote', 'code-block'],  # 引用和代码块
        #     [{'header': 1}, {'header': 2}],  # 标题
        #     [{'list': 'ordered'}, {'list': 'bullet'}],  # 列表
        #     [{'script': 'sub'}, {'script': 'super'}],  # 上下标
        #     [{'size': ['small', False, 'large', 'huge']}],  # 字体大小
        #     ['link', 'image'],  # 链接和图片
        #     ['clean']  # 清除格式
        # ]
    )

    # 更新会话状态
    if content:
        st.session_state.article_content =content

# 右侧预览
with col2:
    st.header("实时预览")
    st.write(st.session_state.article_content, unsafe_allow_html=True)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

不知不道abc

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值