-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
39c5fc3
commit 7f40af0
Showing
10 changed files
with
62 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
docs/src/content/docs/guides/examples.mdx → docs/src/content/docs/guides/demo.mdx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
--- | ||
title: React Integration | ||
description: Use PySandbox in React. | ||
--- | ||
|
||
The easiest way to use `PySandbox` in a React application is by using the `useEffect` hook to execute the Python code. | ||
|
||
```js | ||
import { useEffect, useId, useRef } from "react"; | ||
import { PyMainThreadSandbox } from "pysandbox"; | ||
|
||
function usePython() { | ||
const ref = useRef(); | ||
return { | ||
runPython: async (code, id) => { | ||
if (!ref.current) { | ||
ref.current = new PyMainThreadSandbox(); | ||
} | ||
const sandbox = ref.current; | ||
await sandbox.init(); | ||
if (id) { | ||
document.getElementById(id).replaceChildren(); | ||
} | ||
await sandbox.exec(code, id); | ||
}, | ||
}; | ||
} | ||
|
||
function CodeBlock({ code }) { | ||
const id = useId(); | ||
const { runPython } = usePython(); | ||
useEffect(() => { | ||
runPython(code, id); | ||
}, [code]); | ||
|
||
return <div id={id} />; | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters