Spaces:
Sleeping
Sleeping
Commit
ยท
a96851e
1
Parent(s):
26ee0de
Auto-commit: app.py updated
Browse files
app.py
CHANGED
|
@@ -115,15 +115,75 @@ def execute_with_landrun(language: str, code: str) -> dict:
|
|
| 115 |
if result.stderr:
|
| 116 |
output += f"\n--- STDERR ---\n{result.stderr}"
|
| 117 |
|
| 118 |
-
#
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 122 |
|
| 123 |
return {
|
| 124 |
"output": output or "Execution completed successfully",
|
| 125 |
"exit_code": result.returncode,
|
| 126 |
-
"preview":
|
|
|
|
| 127 |
"security": "๐ Landrun kernel-level isolation active"
|
| 128 |
}
|
| 129 |
|
|
|
|
| 115 |
if result.stderr:
|
| 116 |
output += f"\n--- STDERR ---\n{result.stderr}"
|
| 117 |
|
| 118 |
+
# Generate preview HTML and store with unique ID
|
| 119 |
+
preview_id = str(uuid.uuid4())
|
| 120 |
+
preview_url = None
|
| 121 |
+
preview_html = None
|
| 122 |
+
|
| 123 |
+
# React: Always create preview with JSX
|
| 124 |
+
if language.lower() == "react":
|
| 125 |
+
preview_html = f"""<!DOCTYPE html>
|
| 126 |
+
<html>
|
| 127 |
+
<head>
|
| 128 |
+
<meta charset="UTF-8">
|
| 129 |
+
<script crossorigin src="https://unpkg.com/react@18/umd/react.production.min.js"></script>
|
| 130 |
+
<script crossorigin src="https://unpkg.com/react-dom@18/umd/react-dom.production.min.js"></script>
|
| 131 |
+
<script src="https://unpkg.com/@babel/standalone/babel.min.js"></script>
|
| 132 |
+
<style>body {{ font-family: Arial, sans-serif; padding: 20px; }}</style>
|
| 133 |
+
</head>
|
| 134 |
+
<body>
|
| 135 |
+
<div id="root"></div>
|
| 136 |
+
<script type="text/babel">
|
| 137 |
+
{code}
|
| 138 |
+
const root = ReactDOM.createRoot(document.getElementById('root'));
|
| 139 |
+
root.render(<App />);
|
| 140 |
+
</script>
|
| 141 |
+
</body>
|
| 142 |
+
</html>"""
|
| 143 |
+
|
| 144 |
+
# JavaScript: If code contains HTML-like output, render it
|
| 145 |
+
elif language.lower() == "javascript" and any(tag in code.lower() for tag in ["<div", "<p>", "<h1", "<span", "document."]):
|
| 146 |
+
preview_html = f"""<!DOCTYPE html>
|
| 147 |
+
<html>
|
| 148 |
+
<head>
|
| 149 |
+
<meta charset="UTF-8">
|
| 150 |
+
<style>body {{ font-family: Arial, sans-serif; padding: 20px; }}</style>
|
| 151 |
+
</head>
|
| 152 |
+
<body>
|
| 153 |
+
<div id="output"></div>
|
| 154 |
+
<script>{code}</script>
|
| 155 |
+
</body>
|
| 156 |
+
</html>"""
|
| 157 |
+
|
| 158 |
+
# HTML: Direct rendering
|
| 159 |
+
elif language.lower() == "html":
|
| 160 |
+
preview_html = code
|
| 161 |
+
|
| 162 |
+
# Python: Create visual output if matplotlib/plotting detected
|
| 163 |
+
elif language.lower() == "python":
|
| 164 |
+
if "<html" in output.lower() or "<!doctype" in output.lower():
|
| 165 |
+
preview_html = output
|
| 166 |
+
elif any(lib in code.lower() for lib in ["matplotlib", "plotly", "seaborn"]):
|
| 167 |
+
output += "\n[๐ก Tip: Save plots to HTML for preview]"
|
| 168 |
+
|
| 169 |
+
# Store preview and generate URL
|
| 170 |
+
if preview_html:
|
| 171 |
+
PREVIEW_STORAGE[preview_id] = {
|
| 172 |
+
"html": preview_html,
|
| 173 |
+
"created": datetime.now(),
|
| 174 |
+
"language": language
|
| 175 |
+
}
|
| 176 |
+
# Generate public preview URL
|
| 177 |
+
preview_url = f"/preview/{preview_id}"
|
| 178 |
+
preview_base64 = base64.b64encode(preview_html.encode()).decode()
|
| 179 |
+
else:
|
| 180 |
+
preview_base64 = None
|
| 181 |
|
| 182 |
return {
|
| 183 |
"output": output or "Execution completed successfully",
|
| 184 |
"exit_code": result.returncode,
|
| 185 |
+
"preview": preview_base64,
|
| 186 |
+
"preview_url": preview_url, # NEW: AI agents can GET this URL
|
| 187 |
"security": "๐ Landrun kernel-level isolation active"
|
| 188 |
}
|
| 189 |
|