11# Wokwi Python Client Library
22
3- Typed, asyncio-friendly Python SDK for the ** Wokwi Simulation API** .
3+ Typed Python SDK for the ** Wokwi Simulation API** , with async and sync APIs .
44
55## Features
66
@@ -10,6 +10,7 @@ Typed, asyncio-friendly Python SDK for the **Wokwi Simulation API**.
1010- Monitor serial output asynchronously and write to them
1111- Control peripherals and read GPIO pins
1212- Fully type-annotated and easy to use with asyncio
13+ - Async and sync APIs
1314
1415## Installation
1516
@@ -25,37 +26,74 @@ Get your API token from [https://wokwi.com/dashboard/ci](https://wokwi.com/dashb
2526
2627## Quickstart Example
2728
28- ``` python
29- import asyncio
30- import os
31- from wokwi_client import WokwiClient, GET_TOKEN_URL
32-
33-
34- async def main ():
35- token = os.getenv(" WOKWI_CLI_TOKEN" )
36- if not token:
37- raise SystemExit (
38- f " Set WOKWI_CLI_TOKEN in your environment. You can get it from { GET_TOKEN_URL } . "
39- )
40-
41- client = WokwiClient(token)
42- await client.connect()
43- await client.upload_file(" diagram.json" )
44- await client.upload_file(" firmware.bin" )
45- await client.start_simulation(firmware = " firmware.bin" )
46- serial_task = asyncio.create_task(
47- client.serial_monitor_cat()
48- ) # Stream serial output
49- await client.wait_until_simulation_time(10 ) # Run simulation for 10 seconds
50- serial_task.cancel()
51- await client.disconnect()
52-
53-
54- if __name__ == " __main__" :
55- asyncio.run(main())
56- ```
29+ === "Async (recommended)"
30+
31+ ```python
32+ import asyncio
33+ import os
34+ from wokwi_client import WokwiClient, GET_TOKEN_URL
35+
36+
37+ async def main():
38+ token = os.getenv("WOKWI_CLI_TOKEN")
39+ if not token:
40+ raise SystemExit(
41+ f"Set WOKWI_CLI_TOKEN in your environment. You can get it from {GET_TOKEN_URL}."
42+ )
43+
44+ client = WokwiClient(token)
45+ await client.connect()
46+ await client.upload_file("diagram.json")
47+ await client.upload_file("firmware.bin")
48+ await client.start_simulation(firmware="firmware.bin")
49+
50+ serial_task = asyncio.create_task(
51+ client.serial_monitor_cat()
52+ ) # Stream serial output
53+ await client.wait_until_simulation_time(10) # Run simulation for 10 seconds
54+ serial_task.cancel()
55+ await client.disconnect()
56+
57+
58+ if __name__ == "__main__":
59+ asyncio.run(main())
60+ ```
61+
62+ === "Sync"
63+
64+ ```python
65+ import os
66+ from wokwi_client import WokwiClientSync, GET_TOKEN_URL
67+
68+
69+ def main():
70+ token = os.getenv("WOKWI_CLI_TOKEN")
71+ if not token:
72+ raise SystemExit(
73+ f"Set WOKWI_CLI_TOKEN in your environment. You can get it from {GET_TOKEN_URL}."
74+ )
75+
76+ client = WokwiClientSync(token)
77+ client.connect()
78+ client.upload_file("diagram.json")
79+ client.upload_file("firmware.bin")
80+ client.start_simulation(firmware="firmware.bin")
81+
82+ # Stream serial output concurrently for 10 seconds
83+ client.monitor_serial(lambda line: print(line.decode("utf-8"), end="", flush=True))
84+ client.wait_until_simulation_time(10)
85+ client.disconnect()
86+
87+
88+ if __name__ == "__main__":
89+ main()
90+ ```
91+
92+ ## Examples
5793
58- See the [ examples/hello_esp32/main.py] ( https://github.com/wokwi/wokwi-python-client/blob/main/examples/hello_esp32/main.py ) for a full example including serial monitoring, and [ examples/micropython_esp32/main.py] ( https://github.com/wokwi/wokwi-python-client/blob/main/examples/micropython_esp32/main.py ) for an example of running MicroPython on a simulated ESP32 board.
94+ - [ examples/hello_esp32/main.py] ( https://github.com/wokwi/wokwi-python-client/blob/main/examples/hello_esp32/main.py ) full async example including serial monitoring
95+ - [ examples/hello_esp32_sync/main.py] ( https://github.com/wokwi/wokwi-python-client/blob/main/examples/hello_esp32_sync/main.py ) sync wrapper usage
96+ - [ examples/micropython_esp32/main.py] ( https://github.com/wokwi/wokwi-python-client/blob/main/examples/micropython_esp32/main.py ) example of running MicroPython on a simulated ESP32 board.
5997
6098## API Reference
6199
0 commit comments