|
| 1 | +#!/usr/bin/env python3 |
| 2 | +""" |
| 3 | +Hello World Example - The Simplest Grainchain Usage |
| 4 | +
|
| 5 | +This example demonstrates the absolute simplest way to use grainchain. |
| 6 | +Perfect for getting started in just a few lines of code! |
| 7 | +""" |
| 8 | + |
| 9 | +import asyncio |
| 10 | + |
| 11 | +from grainchain import create_local_sandbox |
| 12 | + |
| 13 | + |
| 14 | +async def hello_world(): |
| 15 | + """The simplest possible grainchain example.""" |
| 16 | + print("🌾 Grainchain Hello World") |
| 17 | + print("=" * 30) |
| 18 | + |
| 19 | + # Create and use a sandbox in just 3 lines! |
| 20 | + sandbox = create_local_sandbox() |
| 21 | + async with sandbox: |
| 22 | + result = await sandbox.execute("echo 'Hello, Grainchain!'") |
| 23 | + print(f"Output: {result.stdout.strip()}") |
| 24 | + |
| 25 | + |
| 26 | +async def hello_python(): |
| 27 | + """Simple Python execution example.""" |
| 28 | + print("\n🐍 Python Hello World") |
| 29 | + print("=" * 30) |
| 30 | + |
| 31 | + sandbox = create_local_sandbox() |
| 32 | + async with sandbox: |
| 33 | + result = await sandbox.execute("python3 -c 'print(\"Hello from Python!\")'") |
| 34 | + print(f"Output: {result.stdout.strip()}") |
| 35 | + |
| 36 | + |
| 37 | +async def hello_file(): |
| 38 | + """Simple file operations example.""" |
| 39 | + print("\n📁 File Operations Hello World") |
| 40 | + print("=" * 30) |
| 41 | + |
| 42 | + sandbox = create_local_sandbox() |
| 43 | + async with sandbox: |
| 44 | + # Create a simple Python script |
| 45 | + script = """ |
| 46 | +print("Hello from a file!") |
| 47 | +print(f"2 + 2 = {2 + 2}") |
| 48 | +""" |
| 49 | + |
| 50 | + # Upload and execute |
| 51 | + await sandbox.upload_file("hello.py", script) |
| 52 | + result = await sandbox.execute("python3 hello.py") |
| 53 | + print("Output:") |
| 54 | + print(result.stdout.strip()) |
| 55 | + |
| 56 | + |
| 57 | +if __name__ == "__main__": |
| 58 | + |
| 59 | + async def main(): |
| 60 | + await hello_world() |
| 61 | + await hello_python() |
| 62 | + await hello_file() |
| 63 | + print("\n✅ All hello world examples completed!") |
| 64 | + |
| 65 | + asyncio.run(main()) |
0 commit comments