@@ -6,7 +6,7 @@ import {usePython} from "react-py";
66
77import PythonIcon from '../../static/img/python-icon.svg'
88import { Tab } from '@headlessui/react'
9- import { ArrowPathIcon , PlayIcon , StopIcon } from '@heroicons/react/24/solid'
9+ import { ArrowPathIcon , PlayIcon , StopIcon , CogIcon } from '@heroicons/react/24/solid'
1010
1111import Input from './Input'
1212import Controls from './Controls'
@@ -68,18 +68,47 @@ const editorOnLoad = editor => {
6868
6969export default function CodeEditor ( props ) {
7070 const [ main , setMain ] = useState ( props . codes ) ;
71+ const [ compareCode , setCompareCode ] = useState ( '' ) ;
72+ const [ envCode , setEnvCode ] = useState ( '' ) ;
73+ const [ headerCode , setHeaderCode ] = useState ( '' ) ;
74+ const [ footerCode , setFooterCode ] = useState ( '' ) ;
75+ const [ isDataLoaded , setIsDataLoaded ] = useState ( false ) ;
7176
7277 useEffect ( ( ) => {
7378 setShowOutput ( false ) ;
7479 setMain ( props . codes ) ;
7580 } , [ props . codes ] ) ;
7681
82+ useEffect ( ( ) => {
83+ const fetchCodes = async ( ) => {
84+ try {
85+ const compareCode = await fetch ( '/codes/python-compare' ) . then ( response => response . text ( ) ) ;
86+ const envCode = await fetch ( '/codes/python-env' ) . then ( response => response . text ( ) ) ;
87+ const headerCode = await fetch ( '/codes/python-header' ) . then ( response => response . text ( ) ) ;
88+ const footerCode = await fetch ( '/codes/python-footer' ) . then ( response => response . text ( ) ) ;
89+
90+ if ( compareCode . startsWith ( '<!DOCTYPE html>' ) || envCode . startsWith ( '<!DOCTYPE html>' ) ) {
91+ throw new Error ( 'Invalid file content: Received HTML instead of Python code' ) ;
92+ }
93+
94+ setCompareCode ( compareCode ) ;
95+ setEnvCode ( envCode ) ;
96+ setHeaderCode ( headerCode ) ;
97+ setFooterCode ( footerCode ) ;
98+ setIsDataLoaded ( true ) ; // 데이터 로드 완료 설정
99+ } catch ( error ) {
100+ console . error ( 'Error fetching Python codes:' , error ) ;
101+ }
102+ } ;
103+ fetchCodes ( ) ;
104+ } , [ ] ) ;
105+
77106 const [ selectedTab , setSelectedTab ] = useState ( 0 ) ;
78107
79108 const handleTabChange = ( index ) => {
80109 setSelectedTab ( index ) ;
81110 } ;
82-
111+
83112 const [ showOutput , setShowOutput ] = useState ( false )
84113
85114 const { colorMode} = useColorMode ( ) ;
@@ -97,7 +126,18 @@ export default function CodeEditor(props) {
97126 } = usePython ( ) ;
98127
99128 function run ( ) {
100- runPython ( main [ selectedTab ] )
129+ const pythonCode = `
130+ ${ headerCode }
131+ ${ props . compare }
132+ ${ main [ selectedTab ] }
133+ func = getattr(Solution(), func_name)
134+
135+ if isinstance(datas[0], tuple):
136+ func(*datas[0])
137+ else:
138+ func(datas[0])
139+ `
140+ runPython ( pythonCode )
101141 setShowOutput ( true )
102142 }
103143
@@ -106,9 +146,24 @@ export default function CodeEditor(props) {
106146 setShowOutput ( false )
107147 }
108148
109- function reset ( ) {
149+ function compare ( ) {
110150 setShowOutput ( false )
111- setMain ( main )
151+ const pythonCode = `
152+ ${ headerCode }
153+ import sys
154+ import types
155+
156+ virtual_files = {
157+ ${ main . filter ( code => code != 'undefined' ) . map ( ( code , i ) => `"${ props . names [ i ] } ": '''${ headerCode } \n${ code } '''` ) . join ( ",\n" ) }
158+ }
159+ ${ envCode }
160+ ${ main . filter ( ( code , i ) => ( code != 'undefined' ) || ( i != selectedTab ) ) . map ( ( code , i ) => `from ${ props . names [ i ] } import Solution as Solution_${ props . names [ i ] } \nsolutions.append(Solution_${ props . names [ i ] } ())` ) . join ( "\n\n" ) }
161+ ${ compareCode }
162+ ${ props . compare }
163+ ${ footerCode }
164+ `
165+ runPython ( pythonCode )
166+ setShowOutput ( true )
112167 }
113168
114169 const updateArrayItem = ( index : number , newValue : string ) => {
@@ -150,10 +205,10 @@ export default function CodeEditor(props) {
150205 } ,
151206 { label : 'Stop' , icon : StopIcon , onClick : stop , hidden : ! isRunning } ,
152207 {
153- label : 'Reset ' ,
154- icon : ArrowPathIcon ,
155- onClick : reset ,
156- disabled : isRunning
208+ label : 'Compare ' ,
209+ icon : CogIcon ,
210+ onClick : compare ,
211+ disabled : isRunning || ! isDataLoaded
157212 }
158213 ] }
159214 isAwaitingInput = { isAwaitingInput }
0 commit comments