Skip to content

Commit

Permalink
basic usage examples
Browse files Browse the repository at this point in the history
  • Loading branch information
shroominic committed Oct 30, 2024
1 parent c46f0b3 commit e114e51
Show file tree
Hide file tree
Showing 2 changed files with 113 additions and 0 deletions.
37 changes: 37 additions & 0 deletions examples/async_example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
from codeboxapi import CodeBox

codebox = CodeBox()


async def async_examples():
# 1. Async Code Execution
result = await codebox.aexec("print('Async Hello!')")
print(result.text)

# 2. Async File Operations
await codebox.aupload("async_file.txt", b"Async content")

downloaded = await codebox.adownload("async_file.txt")
print("File content:", downloaded.get_content())

# 3. All Sync Methods are also available Async
await codebox.ainstall("requests")

# 4. Async Streaming
async for chunk in codebox.astream_exec("""
for i in range(3):
print(f"Async chunk {i}")
import time
time.sleep(1)
"""):
print(chunk.content, end="")

# 5. Async Streaming Download
async for chunk in codebox.astream_download("async_file.txt"):
print(chunk.content)


if __name__ == "__main__":
import asyncio

asyncio.run(async_examples())
76 changes: 76 additions & 0 deletions examples/getting_started.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
from codeboxapi import CodeBox

# Initialize CodeBox
codebox = CodeBox(api_key="local") # or get your API key at https://codeboxapi.com

# Basic Examples
# -------------

# 1. Simple Code Execution
result = codebox.exec("print('Hello World!')")
print(result.text) # Output: Hello World!

# 2. File Operations
# Upload a file
codebox.upload("example.txt", b"Hello from CodeBox!")

# Download a file
downloaded = codebox.download("example.txt")
content = downloaded.get_content() # Returns b"Hello from CodeBox!"

# List files
files = codebox.list_files() # Returns list[RemoteFile]

# 3. Package Management
# Install packages
codebox.install("pandas")

# List installed packages
packages = codebox.list_packages()

# 4. Variable Management
# Execute code that creates variables
codebox.exec("""
x = 42
data = [1, 2, 3]
name = "Alice"
""")

# Show all variables
variables = codebox.show_variables()
print(variables) # Shows dict with all variables and their values

# 5. Plotting with Matplotlib
plot_code = """
import matplotlib.pyplot as plt
plt.figure(figsize=(10, 5))
plt.plot([1, 2, 3, 4], [1, 4, 2, 3])
plt.title('My Plot')
plt.show()
"""
result = codebox.exec(plot_code)
# result.images will contain the plot as bytes

# 6. Streaming Output
# Useful for long-running operations
for chunk in codebox.stream_exec("""
for i in range(5):
print(f"Processing item {i}")
import time
time.sleep(1)
"""):
print(chunk.content, end="")

# 7. Bash Commands
# Execute bash commands
codebox.exec("ls -la", kernel="bash")
codebox.exec("pwd", kernel="bash")

# Create and run Python scripts via bash
codebox.exec("echo \"print('Running from file')\" > script.py", kernel="bash")
codebox.exec("python script.py", kernel="bash")

# 8. Error Handling
result = codebox.exec("1/0")
if result.errors:
print("Error occurred:", result.errors[0])

0 comments on commit e114e51

Please sign in to comment.