Skip to content
Draft
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
144 changes: 144 additions & 0 deletions examples/prime_example.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from traitlets import Any, observe\n",
"from traitlets import Int, Any\n",
"import ipyreact\n",
"\n",
"def is_prime_number(n):\n",
" for i in range(2, n):\n",
" if n % i == 0:\n",
" return \"No 💻🧊🧊🧊\"\n",
" return \"Yes 💻✅✅✅\"\n",
"\n",
"\n",
"class PrimePythonWidget(ipyreact.ReactWidget):\n",
" prime_message = Any(\"Click the Button\").tag(sync=True)\n",
" count = Int(0).tag(sync=True)\n",
"\n",
" @observe(\"count\")\n",
" def _observe_count(self, change):\n",
" self.prime_message = is_prime_number(self.count)\n",
"\n",
" _esm = \"\"\"\n",
" import * as React from \"react\";\n",
"\n",
" export default function({on_count, count, prime_message}) {\n",
" return <div><button onClick={() => on_count(count + 1)}>\n",
" {count} times clicked \n",
" </button>\n",
" <br/>\n",
" <span> {prime_message} </span>\n",
" </div>\n",
" };\"\"\"\n",
"\n",
"\n",
"primepy = PrimePythonWidget()\n",
"primepy\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"primepy.count = 3"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"primepy.count = 4"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"class PrimeJavaScriptWidget(ipyreact.ReactWidget):\n",
" prime_message = Any(\"Click the Button\").tag(sync=True) \n",
" count = Int(0).tag(sync=True)\n",
"\n",
" _esm = \"\"\"\n",
" import * as React from \"react\";\n",
"\n",
" function isPrimeNumber(n) {\n",
" for (let i = 2; i < n; i++) {\n",
" if (n % i === 0) {\n",
" return \"No 🌐🧊🧊🧊\";\n",
" }\n",
" }\n",
" return \"Yes 🌐✅✅✅\";\n",
" }\n",
"\n",
" export const MyUpdater = ({ count, on_count, prime_message }) => {\n",
Comment thread
kolibril13 marked this conversation as resolved.
Outdated
" prime_message = isPrimeNumber(count);\n",
" return <span> {prime_message} </span>;\n",
" };\n",
"\n",
" export default function ({ on_count, count, prime_message }) {\n",
" return (\n",
" <div>\n",
" <button onClick={() => on_count(count + 1)}>{count} times clicked</button>\n",
" <br />\n",
" <MyUpdater count={count} prime_message={prime_message} />\n",
Comment thread
kolibril13 marked this conversation as resolved.
Outdated
" </div>\n",
" );\n",
" }\n",
" \"\"\"\n",
"primejs = PrimeJavaScriptWidget()\n",
"primejs"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"primejs.count = 3"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"primejs.count = 4"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.3"
}
},
"nbformat": 4,
"nbformat_minor": 2
}