#!/usr/bin/env python3 """ LibreChat Pyodide Code Interpreter - Client-Side Python Execution """ import gradio as gr def create_pyodide_interface(): """Create a Gradio interface that uses Pyodide for client-side Python execution""" # Enhanced HTML/JS with better error handling and status updates pyodide_html = """
šŸ”„ Loading Pyodide... This may take 10-30 seconds on first load.
""" return pyodide_html # Create the Gradio interface with gr.Blocks(title="LibreChat Pyodide Code Interpreter") as demo: gr.Markdown("# LibreChat Pyodide Code Interpreter") gr.Markdown("**Client-side Python execution** using Pyodide - Python runs directly in your browser!") gr.Markdown("ā³ **Please wait for Pyodide to fully initialize before executing code (watch the status above).**") # Pyodide interface pyodide_interface = gr.HTML(create_pyodide_interface()) with gr.Row(): with gr.Column(): code_input = gr.Textbox( value="""# Simple test - try this first print("Hello from browser-based Python!") print("2 + 2 =", 2 + 2) # Then try this matplotlib example: # import matplotlib.pyplot as plt # import numpy as np # x = np.linspace(0, 10, 50) # plt.plot(x, np.sin(x)) # plt.title('Sine Wave') # plt.show()""", lines=15, label="Python Code (executes in your browser)" ) execute_btn = gr.Button("Execute with Pyodide", variant="primary") check_status_btn = gr.Button("Check Pyodide Status", variant="secondary") with gr.Column(): gr.Markdown("### Execution Status") status_display = gr.Textbox( label="Last Execution Result", interactive=False, lines=3 ) # JavaScript execution execute_btn.click( fn=None, inputs=[code_input], outputs=[status_display], js=""" function(code) { if (window.executePyodideCode) { return window.executePyodideCode(code); } else { return "Pyodide functions not available. Check browser console for errors."; } } """ ) # Status check button check_status_btn.click( fn=None, inputs=[], outputs=[status_display], js=""" function() { if (window.checkPyodideStatus) { return window.checkPyodideStatus() ? "āœ… Pyodide is ready!" : "ā³ Pyodide is still loading..."; } else { return "āŒ Pyodide not available."; } } """ ) if __name__ == "__main__": demo.launch( server_name="0.0.0.0", server_port=7860, share=False )