1from apify_client import ApifyClient
2
3
4
5client = ApifyClient("<YOUR_API_TOKEN>")
6
7
8run_input = {
9 "code": """# `input` holds the job's JSON input. Set `result` to return data (any JSON-serializable value).
10import statistics
11
12nums = input.get(\"numbers\", [])
13print(f\"Got {len(nums)} numbers\")
14result = {
15 \"count\": len(nums),
16 \"sum\": sum(nums),
17 \"mean\": statistics.mean(nums) if nums else None,
18 \"sorted\": sorted(nums),
19}
20""",
21 "inputJson": { "numbers": [
22 1,
23 2,
24 3,
25 ] },
26 "jobs": [
27 {
28 "id": "python-stats",
29 "input": { "numbers": [
30 4,
31 8,
32 15,
33 16,
34 23,
35 42,
36 ] },
37 },
38 {
39 "id": "js-word-count",
40 "language": "javascript",
41 "code": """// `input` holds the job's JSON input. `return` a value (top-level await works).
42const words = (input.text || '').toLowerCase().match(/[a-z']+/g) || [];
43const counts = {};
44for (const w of words) counts[w] = (counts[w] || 0) + 1;
45console.log(`Counted ${words.length} words`);
46return Object.entries(counts)
47 .sort((a, b) => b[1] - a[1])
48 .slice(0, 5)
49 .map(([word, count]) => ({ word, count }));
50""",
51 "input": { "text": "The quick brown fox jumps over the lazy dog. The dog sleeps; the fox runs." },
52 },
53 ],
54}
55
56
57run = client.actor("gazidev/code-runner-sandbox").call(run_input=run_input)
58
59
60print(f"💾 Check your data here: https://console.apify.com/storage/datasets/{run.default_dataset_id}")
61for item in client.dataset(run.default_dataset_id).iterate_items():
62 print(item)
63
64