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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<!DOCTYPE html>
|
| 2 |
+
<html lang="en">
|
| 3 |
+
<head>
|
| 4 |
+
<meta charset="UTF-8">
|
| 5 |
+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
| 6 |
+
<title>ComfyUI Workflow</title>
|
| 7 |
+
<style>
|
| 8 |
+
body {
|
| 9 |
+
font-family: -apple-system, BlinkMacSystemFont, 'SF Pro Text', sans-serif;
|
| 10 |
+
background-color: #000000;
|
| 11 |
+
color: #f5f5f7;
|
| 12 |
+
padding: 40px;
|
| 13 |
+
}
|
| 14 |
+
pre {
|
| 15 |
+
background: #1d1d1f;
|
| 16 |
+
padding: 24px;
|
| 17 |
+
border-radius: 12px;
|
| 18 |
+
overflow-x: auto;
|
| 19 |
+
}
|
| 20 |
+
</style>
|
| 21 |
+
</head>
|
| 22 |
+
<body>
|
| 23 |
+
<h1>ComfyUI Workflow</h1>
|
| 24 |
+
<p>Error: Invalid JSON format</p>
|
| 25 |
+
<pre>```comfyui
|
| 26 |
+
=== Dockerfile ===
|
| 27 |
+
FROM python:3.11-slim
|
| 28 |
+
|
| 29 |
+
WORKDIR /app
|
| 30 |
+
|
| 31 |
+
COPY requirements.txt .
|
| 32 |
+
|
| 33 |
+
RUN pip install --no-cache-dir -r requirements.txt
|
| 34 |
+
|
| 35 |
+
COPY . .
|
| 36 |
+
|
| 37 |
+
EXPOSE 8000
|
| 38 |
+
|
| 39 |
+
CMD ["python", "main.py"]
|
| 40 |
+
|
| 41 |
+
=== requirements.txt ===
|
| 42 |
+
fastapi==0.109.0
|
| 43 |
+
uvicorn==0.27.0
|
| 44 |
+
python-multipart==0.0.6
|
| 45 |
+
jinja2==3.1.3
|
| 46 |
+
pydantic==2.5.3
|
| 47 |
+
httpx==0.26.0
|
| 48 |
+
|
| 49 |
+
=== package.json ===
|
| 50 |
+
{
|
| 51 |
+
"name": "python-code-generator",
|
| 52 |
+
"version": "1.0.0",
|
| 53 |
+
"private": true,
|
| 54 |
+
"scripts": {
|
| 55 |
+
"dev": "next dev",
|
| 56 |
+
"build": "next build",
|
| 57 |
+
"start": "next start",
|
| 58 |
+
"lint": "next lint"
|
| 59 |
+
},
|
| 60 |
+
"dependencies": {
|
| 61 |
+
"next": "14.1.0",
|
| 62 |
+
"react": "^18",
|
| 63 |
+
"react-dom": "^18",
|
| 64 |
+
"lucide-react": "^0.292.0",
|
| 65 |
+
"clsx": "^2.0.0",
|
| 66 |
+
"tailwind-merge": "^2.0.0",
|
| 67 |
+
"prismjs": "^1.29.0",
|
| 68 |
+
"react-syntax-highlighter": "^11.0.0"
|
| 69 |
+
},
|
| 70 |
+
"devDependencies": {
|
| 71 |
+
"autoprefixer": "^10.0.1",
|
| 72 |
+
"postcss": "^8",
|
| 73 |
+
"tailwindcss": "^3.3.0"
|
| 74 |
+
}
|
| 75 |
+
}
|
| 76 |
+
|
| 77 |
+
=== next.config.js ===
|
| 78 |
+
/** @type {import('next').NextConfig} */
|
| 79 |
+
const nextConfig = {
|
| 80 |
+
reactStrictMode: true,
|
| 81 |
+
api: {
|
| 82 |
+
bodyParser: true,
|
| 83 |
+
}
|
| 84 |
+
}
|
| 85 |
+
|
| 86 |
+
module.exports = nextConfig
|
| 87 |
+
|
| 88 |
+
=== postcss.config.js ===
|
| 89 |
+
module.exports = {
|
| 90 |
+
plugins: {
|
| 91 |
+
tailwindcss: {},
|
| 92 |
+
autoprefixer: {},
|
| 93 |
+
},
|
| 94 |
+
}
|
| 95 |
+
|
| 96 |
+
=== tailwind.config.js ===
|
| 97 |
+
/** @type {import('tailwindcss').Config} */
|
| 98 |
+
module.exports = {
|
| 99 |
+
content: [
|
| 100 |
+
'./pages/**/*.{js,ts,jsx,tsx}',
|
| 101 |
+
'./components/**/*.{js,ts,jsx,tsx}',
|
| 102 |
+
],
|
| 103 |
+
theme: {
|
| 104 |
+
extend: {
|
| 105 |
+
colors: {
|
| 106 |
+
python: {
|
| 107 |
+
blue: '#3776AB',
|
| 108 |
+
yellow: '#FFD43B',
|
| 109 |
+
dark: '#306998',
|
| 110 |
+
light: '#FFE873',
|
| 111 |
+
}
|
| 112 |
+
}
|
| 113 |
+
},
|
| 114 |
+
},
|
| 115 |
+
plugins: [],
|
| 116 |
+
}
|
| 117 |
+
|
| 118 |
+
=== pages/_app.js ===
|
| 119 |
+
import '../styles/globals.css'
|
| 120 |
+
|
| 121 |
+
export default function App({ Component, pageProps }) {
|
| 122 |
+
return <Component {...pageProps} />
|
| 123 |
+
}
|
| 124 |
+
|
| 125 |
+
=== styles/globals.css ===
|
| 126 |
+
@tailwind base;
|
| 127 |
+
@tailwind components;
|
| 128 |
+
@tailwind utilities;
|
| 129 |
+
|
| 130 |
+
body {
|
| 131 |
+
background-color: #f0f4f8;
|
| 132 |
+
color: #1e293b;
|
| 133 |
+
}
|
| 134 |
+
|
| 135 |
+
.code-block {
|
| 136 |
+
background: #1e1e1e;
|
| 137 |
+
border-radius: 8px;
|
| 138 |
+
padding: 16px;
|
| 139 |
+
font-family: 'Courier New', monospace;
|
| 140 |
+
overflow-x: auto;
|
| 141 |
+
}
|
| 142 |
+
|
| 143 |
+
=== pages/index.js ===
|
| 144 |
+
import { useState, useEffect } from 'react';
|
| 145 |
+
import Header from '../components/Header';
|
| 146 |
+
import Sidebar from '../components/Sidebar';
|
| 147 |
+
import CodeList from '../components/CodeList';
|
| 148 |
+
import CodeDisplay from '../components/CodeDisplay';
|
| 149 |
+
import { Code, Play, Download, Copy, CheckCircle2, AlertCircle, Sparkles } from 'lucide-react';
|
| 150 |
+
|
| 151 |
+
const MOCK_CODES = [
|
| 152 |
+
{ id: '1', title: 'FastAPI Hello World', code: 'from fastapi import FastAPI\n\napp = FastAPI()\n\n@app.get("/")\nasync def root():\n return {"message": "Hello World"}\n\nif __name__ == "__main__":\n import uvicorn\n uvicorn.run(app, host="0.0.0.0", port=8000)', date: '10:45 AM', language: 'Python', complexity: 'simple' },
|
| 153 |
+
{ id: '2', title: 'Data Processing Script', code: 'import pandas as pd\nimport numpy as np\n\ndef process_data(file_path):\n df = pd.read_csv(file_path)\n df["processed"] = df["value"] * 2\n return df\n\nif __name__ == "__main__":\n result = process_data("data.csv")\n print(result.head())', date: 'Yesterday', language: 'Python', complexity: 'medium' },
|
| 154 |
+
{ id: '3', title: 'Web Scraper with BeautifulSoup', code: 'import requests\nfrom bs4 import BeautifulSoup\n\ndef scrape_website(url):\n response = requests.get(url)\n soup = BeautifulSoup(response.text, "html.parser")\n title = soup.find("title").text\n return title\n\nif __name__ == "__main__":\n print(scrape_website("https://example.com"))', date: 'Oct 24', language: 'Python', complexity: 'medium' },
|
| 155 |
+
{ id: '4', title: 'Machine Learning Model', code: 'from sklearn.linear_model import LinearRegression\nimport numpy as np\n\nX = np.array([[1], [2], [3], [4]])\ny = np.array([2, 4, 6, 8])\n\nmodel = LinearRegression()\nmodel.fit(X, y)\n\nprint(f"Coefficient: {model.coef_}")\nprint(f"Intercept: {model.intercept_}")', date: 'Oct 23', language: 'Python', complexity: 'complex' },
|
| 156 |
+
{ id: '5', title: 'File Utility Functions', code: 'import os\nimport json\n\ndef read_json(file_path):\n with open(file_path, "r") as f:\n return json.load(f)\n\ndef write_json(file_path, data):\n with open(file_path, "w") as f:\n json.dump(data, f, indent=4)\n\nif __name__ == "__main__":\n data = read_json("config.json")\n write_json("output.json", data)', date: 'Oct 22', language: 'Python', complexity: 'simple' },
|
| 157 |
+
];
|
| 158 |
+
|
| 159 |
+
export default function Home() {
|
| 160 |
+
const [codes, setCodes] = useState(MOCK_CODES);
|
| 161 |
+
const [selectedId, setSelectedId] = useState(null);
|
| 162 |
+
const [searchQuery, setSearchQuery] = useState('');
|
| 163 |
+
const [activeFilter, setActiveFilter] = useState('All');
|
| 164 |
+
const [notification, setNotification] = useState(null);
|
| 165 |
+
const [prompt, setPrompt] = useState('');
|
| 166 |
+
const [isGenerating, setIsGenerating] = useState(false);
|
| 167 |
+
|
| 168 |
+
const filteredCodes = codes.filter(code => {
|
| 169 |
+
const matchesSearch = code.title.toLowerCase().includes(searchQuery.toLowerCase()) ||
|
| 170 |
+
code.code.toLowerCase().includes(searchQuery.toLowerCase());
|
| 171 |
+
const matchesFilter = activeFilter === 'All' || code.complexity === activeFilter;
|
| 172 |
+
return matchesSearch && matchesFilter;
|
| 173 |
+
});
|
| 174 |
+
|
| 175 |
+
const generateCode = async () => {
|
| 176 |
+
if (!prompt.trim()) return;
|
| 177 |
+
setIsGenerating(true);
|
| 178 |
+
|
| 179 |
+
try {
|
| 180 |
+
const response = await fetch('/api/generate', {
|
| 181 |
+
method: 'POST',
|
| 182 |
+
headers: { 'Content-Type': 'application/json' },
|
| 183 |
+
body: JSON.stringify({ prompt: prompt }),
|
| 184 |
+
});
|
| 185 |
+
|
| 186 |
+
const data = await response.json();
|
| 187 |
+
|
| 188 |
+
const newCode = {
|
| 189 |
+
id: String(Date.now()),
|
| 190 |
+
title: prompt.substring(0, 30) + '...',
|
| 191 |
+
code: data.code || '# Generated code placeholder',
|
| 192 |
+
date: 'Now',
|
| 193 |
+
language: 'Python',
|
| 194 |
+
complexity: 'medium'
|
| 195 |
+
};
|
| 196 |
+
|
| 197 |
+
setCodes(prev => [newCode, ...prev]);
|
| 198 |
+
setSelectedId(newCode.id);
|
| 199 |
+
setPrompt('');
|
| 200 |
+
setNotification({ type: 'success', message: 'Code generated successfully!' });
|
| 201 |
+
} catch (error) {
|
| 202 |
+
setNotification({ type: 'error', message: 'Failed to generate code' });
|
| 203 |
+
}
|
| 204 |
+
|
| 205 |
+
setIsGenerating(false);
|
| 206 |
+
setTimeout(() => setNotification(null), 3000);
|
| 207 |
+
};
|
| 208 |
+
|
| 209 |
+
const copyCode = (code) => {
|
| 210 |
+
navigator.clipboard.writeText(code);
|
| 211 |
+
setNotification({ type: 'success', message: 'Code copied to clipboard!' });
|
| 212 |
+
setTimeout(() => setNotification(null), 3000);
|
| 213 |
+
};
|
| 214 |
+
|
| 215 |
+
const downloadCode = (code, title) => {
|
| 216 |
+
const blob = new Blob([code], { type: 'text/plain' });
|
| 217 |
+
const url = URL.createObjectURL(blob);
|
| 218 |
+
const link = document.createElement('a');
|
| 219 |
+
link.href = url;
|
| 220 |
+
link.download = `${title.replace(/\s+/g, '_').toLowerCase()}.py`;
|
| 221 |
+
link.click();
|
| 222 |
+
URL.closeObjectURL(url);
|
| 223 |
+
setNotification({ type: 'success', message: 'Code downloaded!' });
|
| 224 |
+
setTimeout(() => setNotification(null), 3000);
|
| 225 |
+
};
|
| 226 |
+
|
| 227 |
+
const deleteCode = (id) => {
|
| 228 |
+
setCodes(prev => prev.filter(c => c.id !== id));
|
| 229 |
+
if (selectedId === id) setSelectedId(null);
|
| 230 |
+
setNotification({ type: 'success', message: 'Code deleted' });
|
| 231 |
+
setTimeout(() => setNotification(null), 3000);
|
| 232 |
+
};
|
| 233 |
+
|
| 234 |
+
const selectedCode = codes.find(c => c.id === selectedId);
|
| 235 |
+
|
| 236 |
+
return (
|
| 237 |
+
<div className="flex h-screen overflow-hidden bg-slate-50">
|
| 238 |
+
<Sidebar
|
| 239 |
+
activeFilter={activeFilter}
|
| 240 |
+
setActiveFilter={setActiveFilter}
|
| 241 |
+
/>
|
| 242 |
+
|
| 243 |
+
<div className="flex-1 flex flex-col min-w-0">
|
| 244 |
+
<Header
|
| 245 |
+
searchQuery={searchQuery}
|
| 246 |
+
setSearchQuery={setSearchQuery}
|
| 247 |
+
/>
|
| 248 |
+
|
| 249 |
+
<main className="flex-1 flex overflow-hidden">
|
| 250 |
+
<CodeList
|
| 251 |
+
codes={filteredCodes}
|
| 252 |
+
selectedId={selectedId}
|
| 253 |
+
setSelectedId={setSelectedId}
|
| 254 |
+
/>
|
| 255 |
+
|
| 256 |
+
<div className="flex-1 bg-white border-l border-slate-200 overflow-y-auto flex flex-col">
|
| 257 |
+
{selectedCode ? (
|
| 258 |
+
<CodeDisplay
|
| 259 |
+
code={selectedCode}
|
| 260 |
+
onDelete={() => deleteCode(selectedCode.id)}
|
| 261 |
+
onCopy={() => copyCode(selectedCode.code)}
|
| 262 |
+
onDownload={() => downloadCode(selectedCode.code, selectedCode.title)}
|
| 263 |
+
/>
|
| 264 |
+
) : (
|
| 265 |
+
<div className="h-full flex flex-col items-center justify-center text-slate-400 p-8 text-center">
|
| 266 |
+
<div className="bg-slate-100 p-6 rounded-full mb-4">
|
| 267 |
+
<Code size={48} className="text-slate-300" />
|
| 268 |
+
</div>
|
| 269 |
+
<h3 className="text-lg font-medium text-slate-600">No code selected</h3>
|
| 270 |
+
<p className="max-w-xs">Select a code snippet from the list or generate new Python code using the prompt below.</p>
|
| 271 |
+
</div>
|
| 272 |
+
)}
|
| 273 |
+
|
| 274 |
+
<div className="p-6 border-t border-slate-200 bg-slate-50">
|
| 275 |
+
<div className="max-w-3xl mx-auto">
|
| 276 |
+
<label className="block text-sm font-medium text-slate-700 mb-2">Generate New Python Code</label>
|
| 277 |
+
<div className="flex gap-3">
|
| 278 |
+
<input
|
| 279 |
+
type="text"
|
| 280 |
+
placeholder="Describe what you want to generate (e.g., 'Create a function to calculate fibonacci sequence')"
|
| 281 |
+
className="flex-1 px-4 py-3 bg-white border border-slate-300 focus:ring-2 focus:ring-python-blue focus:border-transparent rounded-lg outline-none text-slate-700"
|
| 282 |
+
value={prompt}
|
| 283 |
+
onChange={(e) => setPrompt(e.target.value)}
|
| 284 |
+
disabled={isGenerating}
|
| 285 |
+
/>
|
| 286 |
+
<button
|
| 287 |
+
onClick={generateCode}
|
| 288 |
+
disabled={isGenerating || !prompt.trim()}
|
| 289 |
+
className="px-6 py-3 bg-python-blue hover:bg-blue-700 disabled:bg-slate-400 text-white rounded-lg font-medium transition-colors flex items-center gap-2"
|
| 290 |
+
>
|
| 291 |
+
{isGenerating ? (
|
| 292 |
+
<span className="animate-pulse">Generating...</span>
|
| 293 |
+
) : (
|
| 294 |
+
<>
|
| 295 |
+
<Sparkles size={18} />
|
| 296 |
+
Generate
|
| 297 |
+
</>
|
| 298 |
+
)}
|
| 299 |
+
</button>
|
| 300 |
+
</div>
|
| 301 |
+
</div>
|
| 302 |
+
</div>
|
| 303 |
+
</div>
|
| 304 |
+
</main>
|
| 305 |
+
</div>
|
| 306 |
+
|
| 307 |
+
{notification && (
|
| 308 |
+
<div className="fixed bottom-6 left-1/2 -translate-x-1/2 z-50 flex items-center gap-3 px-4 py-3 rounded-lg shadow-2xl animate-bounce bg-slate-900 text-white border border-slate-700">
|
| 309 |
+
{notification.type === 'success' ? <CheckCircle2 size={18} className="text-green-400" /> : <AlertCircle size={18} className="text-red-400" />}
|
| 310 |
+
<span className="text-sm font-medium">{notification.message}</span>
|
| 311 |
+
</div>
|
| 312 |
+
)}
|
| 313 |
+
</div>
|
| 314 |
+
);
|
| 315 |
+
}
|
| 316 |
+
|
| 317 |
+
=== components/Header.js ===
|
| 318 |
+
import { Search, Menu, Settings, HelpCircle, Grid, Code } from 'lucide-react';
|
| 319 |
+
|
| 320 |
+
export default function Header({ searchQuery, setSearchQuery }) {
|
| 321 |
+
return (
|
| 322 |
+
<header className="h-16 bg-white border-b border-slate-200 flex items-center justify-between px-4 shrink-0">
|
| 323 |
+
<div className="flex items-center gap-4">
|
| 324 |
+
<button className="p-2 hover:bg-slate-100 rounded-full text-slate-600">
|
| 325 |
+
<Menu size={20} />
|
| 326 |
+
</button>
|
| 327 |
+
<div className="flex items-center gap-2">
|
| 328 |
+
<div className="bg-python-blue p-1.5 rounded-lg">
|
| 329 |
+
<Code size={16} className="text-white" />
|
| 330 |
+
</div>
|
| 331 |
+
<span className="text-xl font-semibold text-slate-700 tracking-tight">Python<span className="font-normal text-slate-400">Generator</span></span>
|
| 332 |
+
</div>
|
| 333 |
+
</div>
|
| 334 |
+
|
| 335 |
+
<div className="flex-1 max-w-2xl mx-8">
|
| 336 |
+
<div className="relative group">
|
| 337 |
+
<div className="absolute inset-y-0 left-0 pl-3 flex items-center pointer-events-none">
|
| 338 |
+
<Search size={18} className="text-slate-400 group-focus-within:text-python-blue transition-colors" />
|
| 339 |
+
</div>
|
| 340 |
+
<input
|
| 341 |
+
type="text"
|
| 342 |
+
placeholder="Search code snippets..."
|
| 343 |
+
className="w-full pl-10 pr-4 py-2 bg-slate-100 border-transparent focus:bg-white focus:ring-2 focus:ring-python-blue focus:border-transparent rounded-lg transition-all outline-none text-slate-700"
|
| 344 |
+
value={searchQuery}
|
| 345 |
+
onChange={(e) => setSearchQuery(e.target.value)}
|
| 346 |
+
/>
|
| 347 |
+
</div>
|
| 348 |
+
</div>
|
| 349 |
+
|
| 350 |
+
<div className="flex items-center gap-2">
|
| 351 |
+
<button className="p-2 hover:bg-slate-100 rounded-full text-slate-600 hidden md:block">
|
| 352 |
+
<HelpCircle size={20} />
|
| 353 |
+
</button>
|
| 354 |
+
<button className="p-2 hover:bg-slate-100 rounded-full text-slate-600 hidden md:block">
|
| 355 |
+
<Settings size={20} />
|
| 356 |
+
</button>
|
| 357 |
+
<button className="p-2 hover:bg-slate-100 rounded-full text-slate-600 hidden md:block">
|
| 358 |
+
<Grid size={20} />
|
| 359 |
+
</button>
|
| 360 |
+
<div className="ml-2 w-8 h-8 bg-python-blue rounded-full flex items-center justify-center text-white text-xs font-bold border-2 border-white shadow-sm">
|
| 361 |
+
PY
|
| 362 |
+
</div>
|
| 363 |
+
</div>
|
| 364 |
+
</header>
|
| 365 |
+
);
|
| 366 |
+
}
|
| 367 |
+
|
| 368 |
+
=== components/Sidebar.js ===
|
| 369 |
+
import { Code, Star, Clock, Book, FileCode, Archive, Trash2, Tag } from 'lucide-react';
|
| 370 |
+
|
| 371 |
+
export default function Sidebar({ activeFilter, setActiveFilter }) {
|
| 372 |
+
const menuItems = [
|
| 373 |
+
{ id: 'All', icon: Code, label: 'All Code', count: 12 },
|
| 374 |
+
{ id: 'Starred', icon: Star, label: 'Starred', count: 3 },
|
| 375 |
+
{ id: 'Recent', icon: Clock, label: 'Recent', count: 5 },
|
| 376 |
+
{ id: 'Templates', icon: Book, label: 'Templates', count: 8 },
|
| 377 |
+
{ id: 'Drafts', icon: FileCode, label: 'Drafts', count: 2 },
|
| 378 |
+
];
|
| 379 |
+
|
| 380 |
+
const labels = [
|
| 381 |
+
{ id: 'simple', icon: Tag, color: 'bg-green-500', label: 'Simple' },
|
| 382 |
+
{ id: 'medium', icon: Tag, color: 'bg-yellow-500', label: 'Medium' },
|
| 383 |
+
{ id: 'complex', icon: Tag, color: 'bg-red-500', label: 'Complex' },
|
| 384 |
+
];
|
| 385 |
+
|
| 386 |
+
return (
|
| 387 |
+
<aside className="w-64 bg-slate-50 border-r border-slate-200 flex flex-col shrink-0">
|
| 388 |
+
<div className="p-4">
|
| 389 |
+
<button className="flex items-center gap-3 px-6 py-4 bg-white shadow-md hover:shadow-lg transition-shadow rounded-2xl text-slate-700 font-medium border border-slate-100">
|
| 390 |
+
<Code size={20} className="text-python-blue" />
|
| 391 |
+
New Project
|
| 392 |
+
</button>
|
| 393 |
+
</div>
|
| 394 |
+
|
| 395 |
+
<nav className="flex-1 px-3 space-y-1 overflow-y-auto">
|
| 396 |
+
<div className="mb-4">
|
| 397 |
+
<p className="px-3 text-xs font-semibold text-slate-400 uppercase tracking-wider mb-2">Library</p>
|
| 398 |
+
{menuItems.map((item) => (
|
| 399 |
+
<button
|
| 400 |
+
key={item.id}
|
| 401 |
+
onClick={() => setActiveFilter(item.id)}
|
| 402 |
+
className={`w-full flex items-center justify-between px-3 py-2 rounded-r-full transition-colors text-sm ${
|
| 403 |
+
activeFilter === item.id
|
| 404 |
+
? 'bg-blue-100 text-blue-700 font-semibold'
|
| 405 |
+
: 'text-slate-600 hover:bg-slate-200'
|
| 406 |
+
}`}
|
| 407 |
+
>
|
| 408 |
+
<div className="flex items-center gap-3">
|
| 409 |
+
<item.icon size={18} />
|
| 410 |
+
<span>{item.label}</span>
|
| 411 |
+
</div>
|
| 412 |
+
{item.count > 0 && <span className="text-xs font-medium">{item.count}</span>}
|
| 413 |
+
</button>
|
| 414 |
+
))}
|
| 415 |
+
</div>
|
| 416 |
+
|
| 417 |
+
<div>
|
| 418 |
+
<p className="px-3 text-xs font-semibold text-slate-400 uppercase tracking-wider mb-2">Complexity</p>
|
| 419 |
+
{labels.map((label) => (
|
| 420 |
+
<button
|
| 421 |
+
key={label.id}
|
| 422 |
+
onClick={() => setActiveFilter(label.id)}
|
| 423 |
+
className={`w-full flex items-center gap-3 px-3 py-2 rounded-r-full transition-colors text-sm ${
|
| 424 |
+
activeFilter === label.id
|
| 425 |
+
? 'bg-blue-100 text-blue-700 font-semibold'
|
| 426 |
+
: 'text-slate-600 hover:bg-slate-200'
|
| 427 |
+
}`}
|
| 428 |
+
>
|
| 429 |
+
<label.icon size={18} className={label.color.replace('bg-', 'text-')} />
|
| 430 |
+
<span>{label.label}</span>
|
| 431 |
+
</button>
|
| 432 |
+
))}
|
| 433 |
+
</div>
|
| 434 |
+
</nav>
|
| 435 |
+
|
| 436 |
+
<div className="p-4 border-t border-slate-200">
|
| 437 |
+
<a
|
| 438 |
+
href="https://huggingface.co/spaces/akhaliq/anycoder"
|
| 439 |
+
target="_blank"
|
| 440 |
+
rel="noopener noreferrer"
|
| 441 |
+
className="block text-center text-xs text-slate-400 hover:text-python-blue transition-colors py-2"
|
| 442 |
+
>
|
| 443 |
+
Built with anycoder
|
| 444 |
+
</a>
|
| 445 |
+
</div>
|
| 446 |
+
</aside>
|
| 447 |
+
);
|
| 448 |
+
}
|
| 449 |
+
|
| 450 |
+
=== components/CodeList.js ===
|
| 451 |
+
import { Star, Archive, Trash2, Code } from 'lucide-react';
|
| 452 |
+
|
| 453 |
+
export default function CodeList({ codes, selectedId, setSelectedId }) {
|
| 454 |
+
return (
|
| 455 |
+
<div className="w-full max-w-md bg-white border-r border-slate-200 flex flex-col shrink-0">
|
| 456 |
+
<div className="p-4 border-b border-slate-100 flex items-center justify-between">
|
| 457 |
+
<h2 className="font-semibold text-slate-700">Code Snippets</h2>
|
| 458 |
+
<div className="flex gap-1">
|
| 459 |
+
<button className="p-1.5 hover:bg-slate-100 rounded text-slate-500">
|
| 460 |
+
<Code size={16} />
|
| 461 |
+
</button>
|
| 462 |
+
</div>
|
| 463 |
+
</div>
|
| 464 |
+
|
| 465 |
+
<div className="flex-1 overflow-y-auto">
|
| 466 |
+
{codes.length === 0 ? (
|
| 467 |
+
<div className="p-8 text-center text-slate-400 text-sm">
|
| 468 |
+
No code snippets found matching your search.
|
| 469 |
+
</div>
|
| 470 |
+
) : (
|
| 471 |
+
codes.map((code) => (
|
| 472 |
+
<div
|
| 473 |
+
key={code.id}
|
| 474 |
+
onClick={() => setSelectedId(code.id)}
|
| 475 |
+
className={`group relative p-4 border-b border-slate-100 cursor-pointer transition-all ${
|
| 476 |
+
selectedId === code.id
|
| 477 |
+
? 'bg-blue-50 border-l-4 border-l-python-blue'
|
| 478 |
+
: 'hover:bg-slate-50 border-l-4 border-l-transparent'
|
| 479 |
+
}`}
|
| 480 |
+
>
|
| 481 |
+
<div className="flex justify-between items-start mb-1">
|
| 482 |
+
<span className="text-sm text-slate-900 font-medium">
|
| 483 |
+
{code.title}
|
| 484 |
+
</span>
|
| 485 |
+
<span className="text-xs text-slate-400">{code.date}</span>
|
| 486 |
+
</div>
|
| 487 |
+
<div className="text-xs text-slate-500 truncate mb-1">
|
| 488 |
+
{code.language} · {code.complexity}
|
| 489 |
+
</div>
|
| 490 |
+
<div className="text-xs text-slate-400 truncate font-mono bg-slate-100 p-2 rounded">
|
| 491 |
+
{code.code.substring(0, 50)}...
|
| 492 |
+
</div>
|
| 493 |
+
|
| 494 |
+
<div className="absolute right-2 top-1/2 -translate-y-1/2 opacity-0 group-hover:opacity-100 flex gap-1 transition-opacity">
|
| 495 |
+
<button className="p-1.5 hover:bg-slate-200 rounded-full text-slate-500">
|
| 496 |
+
<Archive size={14} />
|
| 497 |
+
</button>
|
| 498 |
+
<button className="p-1.5 hover:bg-slate-200 rounded-full text-slate-500">
|
| 499 |
+
<Trash2 size={14} />
|
| 500 |
+
</button>
|
| 501 |
+
</div>
|
| 502 |
+
</div>
|
| 503 |
+
))
|
| 504 |
+
)}
|
| 505 |
+
</div>
|
| 506 |
+
</div>
|
| 507 |
+
);
|
| 508 |
+
}
|
| 509 |
+
|
| 510 |
+
=== components/CodeDisplay.js ===
|
| 511 |
+
import { Copy, Download, Trash2, Archive, MoreVertical, Star, Play, Code } from 'lucide-react';
|
| 512 |
+
|
| 513 |
+
export default function CodeDisplay({ code, onDelete, onCopy, onDownload }) {
|
| 514 |
+
return (
|
| 515 |
+
<div className="h-full flex flex-col">
|
| 516 |
+
{/* Toolbar */}
|
| 517 |
+
<div className="p-4 border-b border-slate-200 flex items-center justify-between shrink-0">
|
| 518 |
+
<div className="flex items-center gap-4">
|
| 519 |
+
<button className="p-2 hover:bg-slate-100 rounded-full text-slate-600">
|
| 520 |
+
<Archive size={18} />
|
| 521 |
+
</button>
|
| 522 |
+
<button
|
| 523 |
+
onClick={onDelete}
|
| 524 |
+
className="p-2 hover:bg-red-50 rounded-full text-slate-600 hover:text-red-500 transition-colors"
|
| 525 |
+
>
|
| 526 |
+
<Trash2 size={18} />
|
| 527 |
+
</button>
|
| 528 |
+
<div className="w-px h-6 bg-slate-200 mx-1" />
|
| 529 |
+
<button className="p-2 hover:bg-slate-100 rounded-full text-slate-600">
|
| 530 |
+
<Star size={18} />
|
| 531 |
+
</button>
|
| 532 |
+
<button
|
| 533 |
+
onClick={onCopy}
|
| 534 |
+
className="p-2 hover:bg-slate-100 rounded-full text-slate-600"
|
| 535 |
+
>
|
| 536 |
+
<Copy size={18} />
|
| 537 |
+
</button>
|
| 538 |
+
<button
|
| 539 |
+
onClick={onDownload}
|
| 540 |
+
className="p-2 hover:bg-slate-100 rounded-full text-slate-600"
|
| 541 |
+
>
|
| 542 |
+
<Download size={18} />
|
| 543 |
+
</button>
|
| 544 |
+
</div>
|
| 545 |
+
<div className="flex items-center gap-2">
|
| 546 |
+
<span className="px-3 py-1 bg-python-blue text-white text-xs rounded font-medium">
|
| 547 |
+
{code.language}
|
| 548 |
+
</span>
|
| 549 |
+
<span className={`px-3 py-1 text-xs rounded font-medium ${
|
| 550 |
+
code.complexity === 'complex' ? 'bg-red-100 text-red-600' :
|
| 551 |
+
code.complexity === 'medium' ? 'bg-yellow-100 text-yellow-600' : 'bg-green-100 text-green-600'
|
| 552 |
+
}`}>
|
| 553 |
+
{code.complexity}
|
| 554 |
+
</span>
|
| 555 |
+
<button className="p-2 hover:bg-slate-100 rounded-full text-slate-600">
|
| 556 |
+
<MoreVertical size={18} />
|
| 557 |
+
</button>
|
| 558 |
+
</div>
|
| 559 |
+
</div>
|
| 560 |
+
|
| 561 |
+
{/* Code Content */}
|
| 562 |
+
<div className="flex-1 p-8 overflow-y-auto bg-slate-50">
|
| 563 |
+
<div className="max-w-4xl mx-auto">
|
| 564 |
+
<h1 className="text-2xl font-semibold text-slate-800 mb-4">{code.title}</h1>
|
| 565 |
+
|
| 566 |
+
<div className="flex items-center justify-between mb-6">
|
| 567 |
+
<div className="flex items-center gap-3">
|
| 568 |
+
<div className="w-10 h-10 bg-python-blue rounded-full flex items-center justify-center text-white font-bold">
|
| 569 |
+
<Code size={20} />
|
| 570 |
+
</div>
|
| 571 |
+
<div>
|
| 572 |
+
<div className="font-medium text-slate-900">Generated Code</div>
|
| 573 |
+
<div className="text-xs text-slate-500">Created {code.date}</div>
|
| 574 |
+
</div>
|
| 575 |
+
</div>
|
| 576 |
+
</div>
|
| 577 |
+
|
| 578 |
+
<div className="code-block text-slate-300 leading-relaxed whitespace-pre-wrap mb-8">
|
| 579 |
+
<pre className="font-mono text-sm">
|
| 580 |
+
<code>{code.code}</code>
|
| 581 |
+
</pre>
|
| 582 |
+
</div>
|
| 583 |
+
|
| 584 |
+
{/* Action Buttons */}
|
| 585 |
+
<div className="flex gap-3">
|
| 586 |
+
<button className="flex items-center gap-2 px-6 py-2 border border-slate-300 rounded-full text-slate-600 hover:bg-slate-50 transition-colors font-medium">
|
| 587 |
+
<Play size={16} />
|
| 588 |
+
Run Code
|
| 589 |
+
</button>
|
| 590 |
+
<button
|
| 591 |
+
onClick={onCopy}
|
| 592 |
+
className="flex items-center gap-2 px-6 py-2 border border-slate-300 rounded-full text-slate-600 hover:bg-slate-50 transition-colors font-medium"
|
| 593 |
+
>
|
| 594 |
+
<Copy size={16} />
|
| 595 |
+
Copy
|
| 596 |
+
</button>
|
| 597 |
+
<button
|
| 598 |
+
onClick={onDownload}
|
| 599 |
+
className="flex items-center gap-2 px-6 py-2 bg-python-blue text-white rounded-full hover:bg-blue-700 transition-colors font-medium"
|
| 600 |
+
>
|
| 601 |
+
<Download size={16} />
|
| 602 |
+
Download
|
| 603 |
+
</button>
|
| 604 |
+
</div>
|
| 605 |
+
</div>
|
| 606 |
+
</div>
|
| 607 |
+
</div>
|
| 608 |
+
);
|
| 609 |
+
}
|
| 610 |
+
|
| 611 |
+
=== pages/api/generate.py ===
|
| 612 |
+
from fastapi import FastAPI
|
| 613 |
+
from pydantic import BaseModel
|
| 614 |
+
import json
|
| 615 |
+
|
| 616 |
+
app = FastAPI()
|
| 617 |
+
|
| 618 |
+
class GenerateRequest(BaseModel):
|
| 619 |
+
prompt: str
|
| 620 |
+
|
| 621 |
+
@app.post("/api/generate")
|
| 622 |
+
async def generate_code(request: GenerateRequest):
|
| 623 |
+
# This is a mock implementation
|
| 624 |
+
# In production, this would call an AI code generation API
|
| 625 |
+
prompt = request.prompt.lower()
|
| 626 |
+
|
| 627 |
+
if "hello" in prompt or "world" in prompt:
|
| 628 |
+
code = '''from fastapi import FastAPI
|
| 629 |
+
|
| 630 |
+
app = FastAPI()
|
| 631 |
+
|
| 632 |
+
@app.get("/")
|
| 633 |
+
async def root():
|
| 634 |
+
return {"message": "Hello World"}
|
| 635 |
+
|
| 636 |
+
if __name__ == "__main__":
|
| 637 |
+
import uvicorn
|
| 638 |
+
uvicorn.run(app, host="0.0.0.0", port=8000)'''
|
| 639 |
+
elif "fibonacci" in prompt or "fib" in prompt:
|
| 640 |
+
code = '''def fibonacci(n):
|
| 641 |
+
if n <= 0:
|
| 642 |
+
return 0
|
| 643 |
+
elif n == 1:
|
| 644 |
+
return 1
|
| 645 |
+
else:
|
| 646 |
+
return fibonacci(n-1) + fibonacci(n-2)
|
| 647 |
+
|
| 648 |
+
# Generate first 10 fibonacci numbers
|
| 649 |
+
for i in range(10):
|
| 650 |
+
print(f"F({i}) = {fibonacci(i)}")'''
|
| 651 |
+
elif "file" in prompt or "read" in prompt:
|
| 652 |
+
code = '''def read_file(file_path):
|
| 653 |
+
with open(file_path, "r") as f:
|
| 654 |
+
content = f.read()
|
| 655 |
+
return content
|
| 656 |
+
|
| 657 |
+
def write_file(file_path, content):
|
| 658 |
+
with open(file_path, "w") as f:
|
| 659 |
+
f.write(content)
|
| 660 |
+
|
| 661 |
+
if __name__ == "__main__":
|
| 662 |
+
text = read_file("input.txt")
|
| 663 |
+
write_file("output.txt", text.upper())'''
|
| 664 |
+
else:
|
| 665 |
+
code = f'''# Generated code for: {request.prompt}
|
| 666 |
+
|
| 667 |
+
def generated_function():
|
| 668 |
+
"""
|
| 669 |
+
This function was generated based on your prompt.
|
| 670 |
+
Customize it to fit your needs.
|
| 671 |
+
"""
|
| 672 |
+
print("Hello from generated code!")
|
| 673 |
+
return True
|
| 674 |
+
|
| 675 |
+
if __name__ == "__main__":
|
| 676 |
+
result = generated_function()
|
| 677 |
+
print(f"Result: {result}")'''
|
| 678 |
+
|
| 679 |
+
return {"code": code, "prompt": request.prompt}
|
| 680 |
+
|
| 681 |
+
if __name__ == "__main__":
|
| 682 |
+
import uvicorn
|
| 683 |
+
uvicorn.run(app, host="0.0.0.0", port=8000)
|
| 684 |
+
|
| 685 |
+
=== pages/api/generate.js ===
|
| 686 |
+
export default async function handler(req, res) {
|
| 687 |
+
if (req.method === 'POST') {
|
| 688 |
+
const { prompt } = req.body;
|
| 689 |
+
|
| 690 |
+
// Mock code generation logic
|
| 691 |
+
let code = '';
|
| 692 |
+
|
| 693 |
+
if (prompt.toLowerCase().includes('hello') || prompt.toLowerCase().includes('world')) {
|
| 694 |
+
code = `from fastapi import FastAPI
|
| 695 |
+
|
| 696 |
+
app = FastAPI()
|
| 697 |
+
|
| 698 |
+
@app.get("/")
|
| 699 |
+
async def root():
|
| 700 |
+
return {"message": "Hello World"}
|
| 701 |
+
|
| 702 |
+
if __name__ == "__main__":
|
| 703 |
+
import uvicorn
|
| 704 |
+
uvicorn.run(app, host="0.0.0.0", port=8000)`;
|
| 705 |
+
} else if (prompt.toLowerCase().includes('fibonacci') || prompt.toLowerCase().includes('fib')) {
|
| 706 |
+
code = `def fibonacci(n):
|
| 707 |
+
if n <= 0:
|
| 708 |
+
return 0
|
| 709 |
+
elif n == 1:
|
| 710 |
+
return 1
|
| 711 |
+
else:
|
| 712 |
+
return fibonacci(n-1) + fibonacci(n-2)
|
| 713 |
+
|
| 714 |
+
# Generate first 10 fibonacci numbers
|
| 715 |
+
for i in range(10):
|
| 716 |
+
print(f"F({i}) = {fibonacci(i)})`;
|
| 717 |
+
} else {
|
| 718 |
+
code = `# Generated code for: ${prompt}
|
| 719 |
+
|
| 720 |
+
def generated_function():
|
| 721 |
+
"""
|
| 722 |
+
This function was generated based on your prompt.
|
| 723 |
+
Customize it to fit your needs.
|
| 724 |
+
"""
|
| 725 |
+
print("Hello from generated code!")
|
| 726 |
+
return True
|
| 727 |
+
|
| 728 |
+
if __name__ == "__main__":
|
| 729 |
+
result = generated_function()
|
| 730 |
+
print(f"Result: {result}")`;
|
| 731 |
+
}
|
| 732 |
+
|
| 733 |
+
res.status(200).json({ code, prompt });
|
| 734 |
+
} else {
|
| 735 |
+
res.status(405).json({ error: 'Method not allowed' });
|
| 736 |
+
}
|
| 737 |
+
}
|
| 738 |
+
|
| 739 |
+
=== main.py ===
|
| 740 |
+
from fastapi import FastAPI
|
| 741 |
+
from fastapi.staticfiles import StaticFiles
|
| 742 |
+
from fastapi.responses import FileResponse
|
| 743 |
+
import os
|
| 744 |
+
|
| 745 |
+
app = FastAPI()
|
| 746 |
+
|
| 747 |
+
# Serve static files from Next.js build
|
| 748 |
+
app.mount("/_next", StaticFiles(directory=".next/static", html=True), name="next_static")
|
| 749 |
+
app.mount("/static", StaticFiles(directory="public", html=True), name="static")
|
| 750 |
+
|
| 751 |
+
@app.get("/")
|
| 752 |
+
async def root():
|
| 753 |
+
return FileResponse("public/index.html")
|
| 754 |
+
|
| 755 |
+
@app.post("/api/generate")
|
| 756 |
+
async def generate_code(request: dict):
|
| 757 |
+
prompt = request.get("prompt", "")
|
| 758 |
+
|
| 759 |
+
# Mock code generation logic
|
| 760 |
+
if "hello" in prompt.lower() or "world" in prompt.lower():
|
| 761 |
+
code = '''from fastapi import FastAPI
|
| 762 |
+
|
| 763 |
+
app = FastAPI()
|
| 764 |
+
|
| 765 |
+
@app.get("/")
|
| 766 |
+
async def root():
|
| 767 |
+
return {"message": "Hello World"}
|
| 768 |
+
|
| 769 |
+
if __name__ == "__main__":
|
| 770 |
+
import uvicorn
|
| 771 |
+
uvicorn.run(app, host="0.0.0.0", port=8000)'''
|
| 772 |
+
elif "fibonacci" in prompt.lower() or "fib" in prompt.lower():
|
| 773 |
+
code = '''def fibonacci(n):
|
| 774 |
+
if n <= 0:
|
| 775 |
+
return 0
|
| 776 |
+
elif n == 1:
|
| 777 |
+
return 1
|
| 778 |
+
else:
|
| 779 |
+
return fibonacci(n-1) + fibonacci(n-2)
|
| 780 |
+
|
| 781 |
+
for i in range(10):
|
| 782 |
+
print(f"F({i}) = {fibonacci(i)})'''
|
| 783 |
+
elif "file" in prompt.lower() or "read" in prompt.lower():
|
| 784 |
+
code = '''def read_file(file_path):
|
| 785 |
+
with open(file_path, "r") as f:
|
| 786 |
+
return f.read()
|
| 787 |
+
|
| 788 |
+
def write_file(file_path, content):
|
| 789 |
+
with open(file_path, "w") as f:
|
| 790 |
+
f.write(content)'''
|
| 791 |
+
else:
|
| 792 |
+
code = f'''# Generated code for: {prompt}
|
| 793 |
+
|
| 794 |
+
def generated_function():
|
| 795 |
+
print("Hello from generated code!")
|
| 796 |
+
return True
|
| 797 |
+
|
| 798 |
+
if __name__ == "__main__":
|
| 799 |
+
result = generated_function()
|
| 800 |
+
print(f"Result: {result}")'''
|
| 801 |
+
|
| 802 |
+
return {"code": code, "prompt": prompt}
|
| 803 |
+
|
| 804 |
+
if __name__ == "__main__":
|
| 805 |
+
import uvicorn
|
| 806 |
+
uvicorn.run(app, host="0.0.0.0", port=8000)
|
| 807 |
+
```</pre>
|
| 808 |
+
</body>
|
| 809 |
+
</html>
|