A. 最终效果

B. 可通过鼠标点击打开文件,但会跳转到about:blank
 
import gradio as gr
import subprocess
def open_pptx():
    pptx_path = '/Users/liuguokai/Downloads/240528-工业大模型1.pptx'
    try:
        subprocess.Popen(['open', pptx_path])
        return "PPTX file opened successfully."
    except Exception as e:
        return f"Failed to open PPTX file. Error: {e}"
with gr.Blocks() as demo:
    # Create a clickable HTML component
    html_content = '<a href="javascript:void(0)" οnclick="document.getElementById(\'my_btn\').click()">Open PPTX File</a>'
    html_component = gr.HTML(html_content)
    # Create a hidden button that will be triggered by the HTML link
    action_button = gr.Button(
        value="Open PPTX", elem_id="my_btn", visible=False)
    # Link the button to the Python function
    action_button.click(open_pptx, outputs=None)
    demo.launch(inbrowser=True)
 
C. 可通过鼠标点击打开文件,且会跳转到about:blank
 
为了防止每次点击 HTML 组件后自动跳转到 about:blank,你可以尝试将 href="javascript:void(0)" 替换为 href="#",同时在 onclick 事件中加入 event.preventDefault(),以防止默认的跳转行为。
以下是修改后的代码:
import gradio as gr
import subprocess
def open_pptx():
    pptx_path = '/Users/liuguokai/Downloads/240528-工业大模型1.pptx'
    try:
        subprocess.Popen(['open', pptx_path])
        return "PPTX file opened successfully."
    except Exception as e:
        return f"Failed to open PPTX file. Error: {e}"
with gr.Blocks() as demo:
    # Create a clickable HTML component
    html_content = '''
    <a href="#" οnclick="event.preventDefault(); document.getElementById('my_btn').click()">Open PPTX File</a>
    '''
    html_component = gr.HTML(html_content)
    # Create a hidden button that will be triggered by the HTML link
    action_button = gr.Button(
        value="Open PPTX", elem_id="my_btn", visible=False)
    # Link the button to the Python function
    action_button.click(open_pptx, outputs=None)
    demo.launch(inbrowser=True)
 
说明:
href="#"可以让链接保持为一个有效的锚点,防止浏览器跳转。- 在 
onclick中加入event.preventDefault();用于阻止默认的跳转行为,这样在点击 HTML 组件时就不会跳转到about:blank,只会触发 PPTX 文件的打开操作。 
这个代码改动后,你应该能够实现点击 HTML 组件后仅打开指定的 PPTX 文件,而不会跳转到空白页面。
D. 考虑不同平台兼容性后的代码
当前的代码使用 subprocess.Popen(['open', pptx_path]) 命令在 macOS 上运行。要使其兼容 Windows 和 Linux,需要根据操作系统选择不同的命令来打开文件。
以下是更新后的代码,它可以在 Windows、Linux 和 macOS 上运行:
import gradio as gr
import subprocess
import platform
import os
def open_pptx():
    pptx_path = '/Users/liuguokai/Downloads/240528-工业大模型1.pptx'
    try:
        if platform.system() == 'Darwin':  # macOS
            subprocess.Popen(['open', pptx_path])
        elif platform.system() == 'Windows':  # Windows
            os.startfile(pptx_path)
        elif platform.system() == 'Linux':  # Linux
            subprocess.Popen(['xdg-open', pptx_path])
        return "PPTX file opened successfully."
    except Exception as e:
        return f"Failed to open PPTX file. Error: {e}"
with gr.Blocks() as demo:
    # Create a clickable HTML component
    html_content = '''
    <a href="#" οnclick="event.preventDefault(); document.getElementById('my_btn').click()">Open PPTX File</a>
    '''
    html_component = gr.HTML(html_content)
    # Create a hidden button that will be triggered by the HTML link
    action_button = gr.Button(
        value="Open PPTX", elem_id="my_btn", visible=False)
    # Link the button to the Python function
    action_button.click(open_pptx, outputs=None)
    demo.launch(inbrowser=True)
 
说明:
- macOS: 使用 
open命令来打开文件。 - Windows: 使用 
os.startfile来打开文件。 - Linux: 使用 
xdg-open命令来打开文件,这个命令在大多数 Linux 发行版中都可以使用。 
这样,代码可以在不同的操作系统上正确运行,并根据系统选择相应的命令来打开文件。


















![[Meachines] [Medium] Haircut Curl命令注入+TRP00F自动化权限提升+Screen4.5.0权限提升](https://img-blog.csdnimg.cn/img_convert/c3c3f79f7f659ad8e1f1dc799fca4c96.jpeg)
