You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将git worktree list输出处理后导入Python DataFrame并在Streamlit展示

实现git worktree列表解析并在Streamlit中展示的方案
  • 提前安装依赖:pip install pandas streamlit

以下是完整可直接运行的代码:

import pandas as pd
import streamlit as st
import subprocess
import re

def get_filtered_worktree_df():
    # 调用git命令获取worktree列表原始输出
    result = subprocess.run(
        ["git", "worktree", "list"],
        capture_output=True,
        text=True,
        encoding="utf-8"
    )
    output_lines = result.stdout.strip().split("\n")
    
    # 正则匹配每行的路径、分支信息
    pattern = re.compile(r'^(\S+)\s+\w+\s+(\[.*?\])')
    parsed_data = []
    
    for line in output_lines:
        match = pattern.search(line)
        if not match:
            continue  # 自动跳过detached HEAD、prunable等不符合要求的行
        path, branch = match.groups()
        # 筛选路径末尾为a/b/c的有效条目
        path_suffix = path.rstrip("/").split("/")[-1]
        if path_suffix in {"a", "b", "c"}:
            parsed_data.append({
                "工作树路径": path,
                "检出分支": branch
            })
    
    return pd.DataFrame(parsed_data)

# Streamlit页面渲染
st.title("Git Worktree 有效条目列表")
worktree_df = get_filtered_worktree_df()
st.dataframe(worktree_df, use_container_width=True)

运行代码后会自动过滤无关条目,生成的DataFrame仅包含你需要的两个字段,直接传入Streamlit表格组件即可实现预期展示效果。

内容的提问来源于stack exchange,提问作者arielma

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.23 21:45:04