当前位置: 首页 > article >正文

240907-Gradio插入Mermaid流程图并自适应浏览器高度

A. 最终效果

在这里插入图片描述

B. 示例代码

import gradio as gr

mermaid_code = """
<iframe srcdoc='
<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8" />
		<meta name="viewport" content="width=device-width" />
		<title>My static Space</title>
		<link rel="stylesheet" href="style.css" />
      <script src="https://cdn.jsdelivr.net/npm/mermaid/dist/mermaid.min.js"></script>
      <script>mermaid.initialize({startOnLoad:true});</script>

      <script src="https://cdn.jsdelivr.net/npm/mermaid@10/dist/mermaid.min.js"></script>
<script>
  // 当文档内容加载完成时,初始化并渲染 Mermaid 图表
  document.addEventListener("DOMContentLoaded", function() {
      mermaid.initialize({ startOnLoad: true });
  });
</script> 
	</head>
	<body>

      
<div class="mermaid">
journey
    title Create AI
    section Training
      Format DataSet Inputs Files, Data Splits: 5: Teacher
      Model Build w/ SKLearn, TF, Pytorch: 3: Student
      Determine Model Performance: 1: Teacher, Student
    section Deploy
      Web Deploy Local and Cloud: 5: Teacher
      Architecture Spaces Gradio Streamlit Heroku AWS Azure and GCCP: 5: Teacher
    section Testing
      Test Model with Input Datasets: 5: Teacher
      Examples. Inputs that Work, Inputs That Break Model: 5: Teacher
      Governance - Analyze, Publish Fairness, Equity, Bias for Datasets and Outputs: 5: Teacher
</div>

<div class="mermaid">
sequenceDiagram
    participant Alice
    participant Bob
    Alice->>John: Hello John, how are you?
    loop Healthcheck
        John->>John: Fight against hypochondria
    end
    Note right of John: Rational thoughts<br/>prevail...
    John-->>Alice: Great!
    John->>Bob: How about you?
    Bob-->>John: Jolly good!
</div>
      
<div class="card">
<h1>Welcome to the Mermaid Modeler Tip Sheet</h1>
  <p>
      You can use Mermaid inside HTML5 by including the script and a div with the class or mermaid.
  </p>
  <p>
      Documentation is located here: 
      <a href="https://mermaid.js.org/syntax/flowchart.html" target="_blank"
          >Mermaid documentation</a
      >.
  </p>
</div>


<div class="mermaid">
  graph TD;
    A[开始] --> B{是否正常运行?};
    B -->|是| C[很好];
    B -->|否| D[进行修复];
    D --> A;
</div>




	</body>
</html> ' width="100%" height="800px" style="border:none;">
</iframe>
"""

css = """
/* ⭐️ 流程图的css*/
body {
	padding: 2rem;
	font-family: -apple-system, BlinkMacSystemFont, "Arial", sans-serif;
}

h1 {
	font-size: 16px;
	margin-top: 0;
}

p {
	color: rgb(107, 114, 128);
	font-size: 15px;
	margin-bottom: 10px;
	margin-top: 5px;
}

.card {
	max-width: 620px;
	margin: 0 auto;
	padding: 16px;
	border: 1px solid lightgray;
	border-radius: 16px;
}

.card p:last-child {
	margin-bottom: 0;
}

/* ⭐️ 自适应浏览器高度的css*/
.contain { 
    display: flex; 
    flex-direction: column; 
    height: 95vh; 
    padding: 10px; /* Optional: adjust padding around the container */
    gap: 8px; /* Reduce the gap between components */
}
.gradio-container { 
    height: 100vh !important; 
}
#html-component { 
    flex-grow: 1; 
    overflow: auto; 
}
.fixed-textbox { 
    height: 40px; /* Set a fixed height for the textbox */
    flex-shrink: 0; /* Prevent shrinking */
}
.fixed-button { 
    height: 40px; /* Set a fixed height for the button */
    flex-shrink: 0; /* Prevent shrinking */
}
* {
  border: 1px solid black; /* 设置所有元素的边框宽度为1px,样式为实线,颜色为黑色 */
}
"""


def display_diagram():
    return mermaid_code


with gr.Blocks(css=css) as demo:
    with gr.Row():
        with gr.Column(elem_classes="contain"):
            gr.HTML(mermaid_code, elem_id='html-container')
        with gr.Column(elem_classes="contain"):
            html = gr.HTML(
                mermaid_code, elem_id="html-component")
            msg = gr.Textbox()
            clear = gr.Button("Clear",  elem_classes="fixed-button")

demo.launch()

C. 参考文献

  • Allow gr.Chatbot to fill all height of rest of space · Issue #4001 · gradio-app/gradio
  • mariashay/DataViz-Mermaid at main

http://www.kler.cn/a/300115.html

相关文章:

  • Plotly 函数图像绘制
  • tinykv Project2ab 实现思路
  • CentOS 7乱码问题如何解决?
  • 26考研资料分享 百度网盘
  • 基于微信小程序的科创微应用平台设计与实现(LW+源码+讲解)
  • 【Linux】环境变量
  • 2.2.3 UDP的可靠传输协议QUIC 2
  • 【读书笔记-《30天自制操作系统》-19】Day20
  • 【Python知识宝库】迭代器与生成器:高效处理大数据集
  • SQL的高级查询练习知识点中(day25)
  • 认识向量数据
  • 【Docker系列】docker缓存详解
  • 【每日一题】LeetCode 2181.合并零之间的节点(链表、模拟)
  • HAL库学习目录查询表
  • 在WPF中集成OpenTK画图形
  • google vr 入门之VrPanoramaView制作全景图列表(1)
  • 深入理解Spring Boot结合MyBatis调用MySQL,并实现主从复制读写分离
  • Linux进阶命令-echodatealias
  • redis底层—网络模型
  • 在社交物联网中使用MQTT协议和Hardy Wall算法实现有效的多播通信
  • [论文笔记]ChatQA: Surpassing GPT-4 on Conversational QA and RAG
  • 分布式项目中使用雪花算法提前获取对象主键ID
  • 非关系型数据库Redis
  • PDF和CDF
  • 8.Bug流程管理,禅道的使用(包含笔试/面试题)
  • Linux 工程师:探索开源世界的专业之路