File size: 12,171 Bytes
edc1213 8cce585 edc1213 8cce585 edc1213 b767c50 ba77ed1 b767c50 ba77ed1 b767c50 ba77ed1 b767c50 ba77ed1 b767c50 ba77ed1 b767c50 ba77ed1 edc1213 b767c50 edc1213 ba77ed1 edc1213 ba77ed1 edc1213 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 | <!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Python Playground</title>
<link rel="icon" type="image/x-icon" href="/static/favicon.ico">
<script src="https://cdn.tailwindcss.com"></script>
<script src="https://unpkg.com/feather-icons"></script>
<style>
.code-block {
font-family: 'Courier New', monospace;
background-color: #f8f8f8;
border-radius: 4px;
padding: 16px;
overflow-x: auto;
}
.python-keyword { color: #0000ff; }
.python-string { color: #a31515; }
.python-comment { color: #008000; }
.python-builtin { color: #795e26; }
</style>
</head>
<body class="bg-gray-50 min-h-screen">
<div class="container mx-auto px-4 py-8">
<header class="mb-12">
<div class="flex items-center justify-between">
<div class="flex items-center space-x-4">
<div class="w-12 h-12 rounded-full bg-yellow-400 flex items-center justify-center text-white">
<i data-feather="code" class="w-6 h-6"></i>
</div>
<h1 class="text-3xl font-bold text-gray-800">Python Playground</h1>
</div>
<a href="builder.html" class="px-4 py-2 bg-blue-500 text-white rounded-md hover:bg-blue-600 flex items-center">
<i data-feather="layout" class="w-4 h-4 mr-2"></i>
Site Builder
</a>
</div>
<p class="mt-2 text-gray-600">Explore and run Python code directly in your browser</p>
</header>
<div class="grid grid-cols-1 lg:grid-cols-2 gap-8">
<div class="bg-white rounded-xl shadow-md p-6">
<h2 class="text-xl font-semibold mb-4 text-gray-800">Code Editor</h2>
<textarea id="python-code" class="w-full h-96 p-4 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500 font-mono" spellcheck="false">
# Trainable Chat Bot
knowledge_base = {}
def train_bot():
print("\nBot: Teach me something new!")
question = input("You say: ")
answer = input("I respond: ")
knowledge_base[question.lower()] = answer
print("Bot: Got it! I've learned something new.")
def save_knowledge():
with open('bot_knowledge.txt', 'w') as f:
for q, a in knowledge_base.items():
f.write(f"{q}|{a}\n")
print("Bot: My knowledge has been saved!")
def load_knowledge():
try:
with open('bot_knowledge.txt', 'r') as f:
for line in f:
q, a = line.strip().split('|')
knowledge_base[q] = a
except FileNotFoundError:
pass
def chat_bot():
load_knowledge()
print("Bot: Hi there! I'm a trainable BotBuddy. Type 'train' to teach me or 'quit' to exit.")
while True:
message = input("\nYou: ").lower()
if message == 'quit':
save_knowledge()
print("Bot: Goodbye! Thanks for chatting.")
break
elif message == 'train':
train_bot()
elif message in knowledge_base:
print(f"Bot: {knowledge_base[message]}")
else:
print("Bot: I don't know how to respond to that. Type 'train' to teach me!")
chat_bot()
</textarea>
<div class="mt-4 flex space-x-3">
<button id="run-btn" class="px-6 py-2 bg-green-500 text-white rounded-lg hover:bg-green-600 flex items-center">
<i data-feather="play" class="w-4 h-4 mr-2"></i>
Run Code
</button>
<button id="clear-btn" class="px-6 py-2 bg-gray-200 text-gray-700 rounded-lg hover:bg-gray-300 flex items-center">
<i data-feather="trash-2" class="w-4 h-4 mr-2"></i>
Clear
</button>
</div>
<div class="mt-4 bg-blue-50 p-4 rounded-lg">
<h3 class="font-medium text-blue-800 mb-2">How to use this bot:</h3>
<ul class="list-disc pl-5 space-y-1 text-blue-700">
<li>Type normal messages to chat</li>
<li>Type 'train' to teach the bot new responses</li>
<li>Type 'quit' to end the session</li>
<li>The bot saves everything you teach it!</li>
</ul>
</div>
</div>
<div class="bg-white rounded-xl shadow-md p-6">
<h2 class="text-xl font-semibold mb-4 text-gray-800">Output</h2>
<div id="output" class="h-96 bg-black text-green-400 p-4 rounded-lg overflow-y-auto font-mono">
<p class="text-gray-400">Your output will appear here...</p>
</div>
<div class="mt-4">
<h3 class="text-lg font-medium mb-2 text-gray-700">Python Resources</h3>
<ul class="space-y-2">
<li><a href="https://docs.python.org/3/" target="_blank" class="text-blue-500 hover:underline">Python Documentation</a></li>
<li><a href="https://realpython.com/" target="_blank" class="text-blue-500 hover:underline">Real Python Tutorials</a></li>
<li><a href="https://www.pythoncheatsheet.org/" target="_blank" class="text-blue-500 hover:underline">Python Cheat Sheet</a></li>
</ul>
</div>
</div>
</div>
<div class="mt-12 bg-white rounded-xl shadow-md p-6">
<h2 class="text-xl font-semibold mb-4 text-gray-800">Example Snippets</h2>
<div class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-4">
<div class="border border-gray-200 rounded-lg p-4">
<h3 class="font-medium text-gray-800 mb-2">Fibonacci Sequence</h3>
<div class="code-block">
<span class="python-keyword">def</span> fib(n):<br>
<span class="python-keyword">if</span> n <= <span class="python-builtin">1</span>:<br>
<span class="python-keyword">return</span> n<br>
<span class="python-keyword">return</span> fib(n-<span class="python-builtin">1</span>) + fib(n-<span class="python-builtin">2</span>)<br><br>
<span class="python-comment"># Print first 10 Fibonacci numbers</span><br>
<span class="python-keyword">for</span> i <span class="python-keyword">in</span> <span class="python-builtin">range</span>(<span class="python-builtin">10</span>):<br>
<span class="python-builtin">print</span>(fib(i))
</div>
</div>
<div class="border border-gray-200 rounded-lg p-4">
<h3 class="font-medium text-gray-800 mb-2">List Comprehension</h3>
<div class="code-block">
<span class="python-comment"># Squares of even numbers</span><br>
numbers = [<span class="python-builtin">1</span>, <span class="python-builtin">2</span>, <span class="python-builtin">3</span>, <span class="python-builtin">4</span>, <span class="python-builtin">5</span>, <span class="python-builtin">6</span>]<br>
squares = [x**<span class="python-builtin">2</span> <span class="python-keyword">for</span> x <span class="python-keyword">in</span> numbers <span class="python-keyword">if</span> x % <span class="python-builtin">2</span> == <span class="python-builtin">0</span>]<br><br>
<span class="python-builtin">print</span>(squares) <span class="python-comment"># Output: [4, 16, 36]</span>
</div>
</div>
<div class="border border-gray-200 rounded-lg p-4">
<h3 class="font-medium text-gray-800 mb-2">API Request</h3>
<div class="code-block">
<span class="python-keyword">import</span> requests<br><br>
<span class="python-comment"># Fetch data from JSONPlaceholder API</span><br>
response = requests.get(<span class="python-string">"https://jsonplaceholder.typicode.com/todos/1"</span>)<br>
data = response.json()<br><br>
<span class="python-builtin">print</span>(<span class="python-string">"Todo:"</span>, data[<span class="python-string">"title"</span>])<br>
<span class="python-builtin">print</span>(<span class="python-string">"Completed:"</span>, data[<span class="python-string">"completed"</span>])
</div>
</div>
</div>
</div>
</div>
<script>
</script>
</body>
</html> |