import { useState, useEffect } from 'react' import { motion } from 'framer-motion' import { Play, CircleNotch, Terminal } from '@phosphor-icons/react' import { Button } from '@/components/ui/button' import { Card } from '@/components/ui/card' import { Tabs, TabsContent, TabsList, TabsTrigger } from '@/components/ui/tabs' import { runPythonCode, getPyodide, isPyodideReady } from '@/lib/pyodide-runner' import { PythonTerminal } from '@/components/features/python-runner/PythonTerminal' import { toast } from 'sonner' interface PythonOutputProps { code: string } export function PythonOutput({ code }: PythonOutputProps) { const [output, setOutput] = useState('') const [error, setError] = useState('') const [isRunning, setIsRunning] = useState(false) const [isInitializing, setIsInitializing] = useState(!isPyodideReady()) const [hasInput, setHasInput] = useState(false) useEffect(() => { if (!isPyodideReady()) { setIsInitializing(true) getPyodide() .then(() => { setIsInitializing(false) toast.success('Python environment ready!') }) .catch((err) => { setIsInitializing(false) toast.error('Failed to load Python environment') console.error(err) }) } }, []) useEffect(() => { const codeToCheck = code.toLowerCase() setHasInput(codeToCheck.includes('input(')) }, [code]) const handleRun = async () => { if (isInitializing) { toast.info('Python environment is still loading...') return } setIsRunning(true) setOutput('') setError('') try { const result = await runPythonCode(code) setOutput(result.output || '') if (result.error) { setError(result.error) } } catch (err) { setError(err instanceof Error ? err.message : String(err)) } finally { setIsRunning(false) } } if (hasInput) { return } return (

Python Output

{!output && !error && (
Click "Run" to execute the Python code
)} {output && (
                {output}
              
)} {error && (
Error:
                  {error}
                
)}
) }