Tutorial.md (1398B)
1 # How to use Pytml? 2 3 Pytml lets you run Python directly in HTML with one script tag. 4 5 ## 1. Start 6 7 ```html 8 <script src="https://pytml.vercel.app/pytml.js"></script> 9 ``` 10 11 ## 2. Write Python 12 13 ```html 14 <py> 15 print("Hello from Pytml!") 16 </py> 17 ``` 18 19 ## 3. Add HTML controls 20 21 ```html 22 <button id="hello">Click me</button> 23 <p id="output"></p> 24 25 <py> 26 def hello(event): 27 output.text = "Python handled the click." 28 29 hello.on("click", hello) 30 </py> 31 ``` 32 33 ## 4. Use numbered Pytml controls 34 35 ```html 36 <py1> 37 38 <btn1>1</btn1> 39 <btn2>2</btn2> 40 41 number = "" 42 43 def press(n): 44 global number 45 number += str(n) 46 47 btn1.on("click", lambda e: press(1)) 48 btn2.on("click", lambda e: press(2)) 49 50 </py1> 51 ``` 52 53 ## 5. Multiple programs 54 55 `py1`, `py2`, and higher numbers are program identifiers. Repeating the same identifier shares that program's state. 56 57 See `demo.html` and the `examples/` directory for runnable examples. 58 59 60 ### Local `file://` pages 61 62 You can open an HTML file directly from your computer and still load Pytml from the public HTTPS URL: 63 64 ```html 65 <script src="https://pytml.vercel.app/pytml.js"></script> 66 ``` 67 68 Inline `<py>` and `<pyN>` programs work in this mode. `local-file-test.html` is a ready-to-open example. A relative Python source file such as `<py src="app.py">` cannot be fetched from `file://` because browsers block normal JavaScript access to local files; use inline Python or an HTTPS source URL instead.