Skip to content

armgit / E2B

Public Imported from GitHub
0

Open-source, secure environment with real-world tools for enterprise-grade agents.

Add JS code examples

Vasek Mlejnsky27abf533752c
diff --git a/apps/docs/src/app/getting-started/basics/page.mdx b/apps/docs/src/app/getting-started/basics/page.mdx
index 37f4242a2..810e12b4b 100644
--- a/apps/docs/src/app/getting-started/basics/page.mdx
+++ b/apps/docs/src/app/getting-started/basics/page.mdx
@@ -12,7 +12,6 @@ export const sections = [
# Basic Usage
-
Following code snippets should get you going fast with primitives in our SDK.
{/* For more real-life scenarios head to the [Examples section](./examples/overview). {{ className: 'lead' }} */}
@@ -20,10 +19,12 @@ There are two main primitives in our SDK that you use to interact with the cloud
- [Filesystem](/getting-started/basics#filesystem)
- [Process](/getting-started/basics#process)
-## Create Cloud Playground
+## Create AI Playground
<CodeGroup>
```python {{ language: 'python', snippet: 'python/basics/init.py' }}
```
+```js {{ language: 'js', snippet: 'js/basics/init.js' }}
+```
</CodeGroup>
## Filesystem
@@ -40,6 +41,8 @@ Here are the basic operations you can do with the filesystem inside a cloud play
<CodeGroup>
```python {{ language: 'python', snippet: 'python/basics/fs_ls.py' }}
```
+```js {{ language: 'js', snippet: 'js/basics/fs_ls.js' }}
+```
</CodeGroup>
## Create directory
@@ -50,6 +53,8 @@ For example, if you want to create a directory `/home/user/new_dir` and `/home/u
<CodeGroup>
```python {{ language: 'python', snippet: 'python/basics/fs_mkdir.py' }}
```
+```js {{ language: 'js', snippet: 'js/basics/fs_mkdir.js' }}
+```
</CodeGroup>
## Write to file
@@ -62,22 +67,27 @@ When writing to a file that's in a directory that doesn't exist, you'll get an e
<CodeGroup>
```python {{ language: 'python', snippet: 'python/basics/fs_write.py' }}
```
+```js {{ language: 'js', snippet: 'js/basics/fs_write.js' }}
+```
</CodeGroup>
## Read file
<CodeGroup>
```python {{ language: 'python', snippet: 'python/basics/fs_read.py' }}
```
+```js {{ language: 'js', snippet: 'js/basics/fs_read.js' }}
+```
</CodeGroup>
## Watch directory for changes
<CodeGroup>
```python {{ language: 'python', snippet: 'python/basics/fs_watch.py' }}
```
+```js {{ language: 'js', snippet: 'js/basics/fs_watch.js' }}
+```
</CodeGroup>
## Process
-
Here are the basic operations you can do with the process inside a cloud playground:
- [Start process](/getting-started/basics#start-process)
- [Stop process](/getting-started/basics#stop-process)
@@ -89,28 +99,38 @@ Here are the basic operations you can do with the process inside a cloud playgro
<CodeGroup>
```python {{ language: 'python', snippet: 'python/basics/process_start.py' }}
```
+```js {{ language: 'js', snippet: 'js/basics/process_start.js' }}
+```
</CodeGroup>
## Stop process
<CodeGroup>
```python {{ language: 'python', snippet: 'python/basics/process_stop.py' }}
```
+```js {{ language: 'js', snippet: 'js/basics/process_stop.js' }}
+```
</CodeGroup>
## Stream stdout
<CodeGroup>
```python {{ language: 'python', snippet: 'python/basics/process_stream_stdout.py' }}
```
+```js {{ language: 'js', snippet: 'js/basics/process_stream_stdout.js' }}
+```
</CodeGroup>
## Send stdin
<CodeGroup>
```python {{ language: 'python', snippet: 'python/basics/process_write_stdin.py' }}
```
+```js {{ language: 'js', snippet: 'js/basics/process_write_stdin.js' }}
+```
</CodeGroup>
## Stream stderr
<CodeGroup>
```python {{ language: 'python', snippet: 'python/basics/process_stream_stderr.py' }}
```
+```js {{ language: 'js', snippet: 'js/basics/process_stream_stderr.js' }}
+```
</CodeGroup>
diff --git a/apps/docs/src/code/js/basics/init.js b/apps/docs/src/code/js/basics/init.js
index 5b17c7510..80ac63280 100644
--- a/apps/docs/src/code/js/basics/init.js
+++ b/apps/docs/src/code/js/basics/init.js
@@ -7,5 +7,4 @@ const session = await Session.create({
id: 'Nodejs',
apiKey: process.env.E2B_API_KEY,
})
-
await session.close()
\ No newline at end of file
diff --git a/apps/docs/src/code/js/run_all.js b/apps/docs/src/code/js/run_all.js
new file mode 100644
index 000000000..7f6bcd1fc
--- /dev/null
+++ b/apps/docs/src/code/js/run_all.js
@@ -0,0 +1,14 @@
+import * as quickstart from './quickstart.js'
+import * as init from './basics/init.js'
+import * as fs_ls from './basics/fs_ls.js'
+import * as fs_mkdir from './basics/fs_mkdir.js'
+import * as fs_read from './basics/fs_read.js'
+import * as fs_watch from './basics/fs_watch.js'
+import * as fs_write from './basics/fs_write.js'
+import * as process_start from './basics/process_start.js'
+import * as process_stop from './basics/process_stop.js'
+import * as process_stream_stdout from './basics/process_stream_stdout.js'
+import * as process_stream_stderr from './basics/process_stream_stderr.js'
+import * as process_write_stdin from './basics/process_write_stdin.js'
+
+console.log('Done')
\ No newline at end of file
diff --git a/apps/docs/src/code/python/_run_all.py b/apps/docs/src/code/python/_run_all.py
new file mode 100644
index 000000000..ae633db68
--- /dev/null
+++ b/apps/docs/src/code/python/_run_all.py
@@ -0,0 +1,14 @@
+from quickstart import main as quickstart_main
+from basics.init import main as init_main
+from basics.fs_ls import main as fs_ls_main
+from basics.fs_mkdir import main as fs_mkdir_main
+from basics.fs_read import main as fs_read_main
+from basics.fs_write import main as fs_write_main
+from basics.fs_watch import main as fs_watch_main
+from basics.process_start import main as process_start_main
+from basics.process_stop import main as process_stop_main
+from basics.process_stream_stderr import main as process_stream_stderr_main
+from basics.process_stream_stdout import main as process_stream_stdout_main
+from basics.process_write_stdin import main as process_write_stdin_main
+
+print("Done")
\ No newline at end of file
diff --git a/apps/docs/src/code/python/basics/__init__.py b/apps/docs/src/code/python/basics/__init__.py
new file mode 100644
index 000000000..e69de29bb
diff --git a/apps/docs/src/code/python/basics/_run_all.py b/apps/docs/src/code/python/basics/_run_all.py
deleted file mode 100644
index 3b3b28c22..000000000
--- a/apps/docs/src/code/python/basics/_run_all.py
+++ /dev/null
@@ -1,30 +0,0 @@
-import asyncio
-from init import main as init_main
-from fs_ls import main as fs_ls_main
-from fs_mkdir import main as fs_mkdir_main
-from fs_read import main as fs_read_main
-from fs_write import main as fs_write_main
-from fs_watch import main as fs_watch_main
-from process_start import main as process_start_main
-from process_stop import main as process_stop_main
-from process_stream_stderr import main as process_stream_stderr_main
-from process_stream_stdout import main as process_stream_stdout_main
-from process_write_stdin import main as process_write_stdin_main
-
-async def main():
- tasks = [
- fs_ls_main(),
- init_main(),
- fs_mkdir_main(),
- fs_read_main(),
- fs_write_main(),
- fs_watch_main(),
- process_start_main(),
- process_stop_main(),
- process_stream_stderr_main(),
- process_stream_stdout_main(),
- process_write_stdin_main()
- ]
- await asyncio.gather(*tasks)
-
-asyncio.run(main())
diff --git a/apps/docs/src/code/python/basics/fs_watch.py b/apps/docs/src/code/python/basics/fs_watch.py
index 33daabe1f..568148d16 100644
--- a/apps/docs/src/code/python/basics/fs_watch.py
+++ b/apps/docs/src/code/python/basics/fs_watch.py
@@ -6,6 +6,7 @@ E2B_API_KEY = getenv("E2B_API_KEY")
watcher = None
async def create_watcher(session):
+ # Start filesystem watcher for the /home directory
watcher = await session.filesystem.watch_dir("/home")
watcher.add_event_listener(lambda event: print(event))
await watcher.start()
@@ -15,7 +16,12 @@ async def main():
await create_watcher(session)
+ # Create files in the /home directory inside the playground
+ # We'll receive notifications for these events through the watcher we created above.
for i in range(10):
+ # `filesystem.write()` will trigger two events:
+ # 1. 'Create' when the file is created
+ # 2. 'Write' when the file is written to
await session.filesystem.write(f"/home/file{i}.txt", f"Hello World {i}!")
await asyncio.sleep(1)
diff --git a/apps/docs/src/code/python/basics/init.py b/apps/docs/src/code/python/basics/init.py
index ad28824c6..81f955561 100644
--- a/apps/docs/src/code/python/basics/init.py
+++ b/apps/docs/src/code/python/basics/init.py
@@ -5,6 +5,9 @@ from e2b import Session
E2B_API_KEY = getenv("E2B_API_KEY")
async def main():
+ # `id` can also be one of:
+ # 'Nodejs', 'Bash', 'Python3', 'Java', 'Go', 'Rust', 'PHP', 'Perl', 'DotNET'
+ # We're working on custom environments.
session = await Session.create(id="Nodejs", api_key=E2B_API_KEY)
await session.close()
diff --git a/apps/docs/src/code/python/basics/process_write_stdin.py b/apps/docs/src/code/python/basics/process_write_stdin.py
index e460b1b25..5ab861675 100644
--- a/apps/docs/src/code/python/basics/process_write_stdin.py
+++ b/apps/docs/src/code/python/basics/process_write_stdin.py
@@ -7,13 +7,13 @@ E2B_API_KEY = getenv("E2B_API_KEY")
async def main():
session = await Session.create(id="Nodejs", api_key=E2B_API_KEY)
- # This example will print back the string we send to the process using `send_stdin`
+ # This example will print back the string we send to the process using `send_stdin()`
proc = await session.process.start(
"while IFS= read -r line; do echo \"$line\"; sleep 1; done",
on_stdout=print,
)
- await proc.send_stdin("marco\n")
+ await proc.send_stdin("AI Playground\n")
await proc.kill()
await session.close()
diff --git a/apps/docs/src/code/python/quickstart.py b/apps/docs/src/code/python/quickstart.py
index 2f0947ec1..81f955561 100644
--- a/apps/docs/src/code/python/quickstart.py
+++ b/apps/docs/src/code/python/quickstart.py
@@ -11,4 +11,4 @@ async def main():
session = await Session.create(id="Nodejs", api_key=E2B_API_KEY)
await session.close()
-asyncio.new_event_loop().run_until_complete(main())
\ No newline at end of file
+asyncio.run(main())
\ No newline at end of file
diff --git a/apps/docs/src/mdx/remark.mjs b/apps/docs/src/mdx/remark.mjs
index 2e1b6a758..327d4bed4 100644
--- a/apps/docs/src/mdx/remark.mjs
+++ b/apps/docs/src/mdx/remark.mjs
@@ -29,7 +29,7 @@ function loadFileSnippet() {
(async () => {
const file = path.resolve(`./src/code/${snippetFileName}`)
const content = await readFile(file, 'utf8')
- node.value = content
+ node.value = content.trim()
})()
)
}