|
| 1 | +## Embedding gpython |
| 2 | + |
| 3 | +This is an example demonstrating how to embed gpython into a Go application. |
| 4 | + |
| 5 | + |
| 6 | +### Why embed gpython? |
| 7 | + |
| 8 | +Embedding a highly capable and familiar "interpreted" language allows your users |
| 9 | +to easily augment app behavior, configuration, and customization -- all post-deployment. |
| 10 | + |
| 11 | +Have you ever discovered an exciting software project but lost interest when you had to also |
| 12 | +learn its esoteric language schema? In an era of limited attention span, |
| 13 | +most people are generally turned off if they have to learn a new language in addition to learning |
| 14 | +to use your app. |
| 15 | + |
| 16 | +If you consider [why use Python](https://www.stxnext.com/what-is-python-used-for/), then perhaps also |
| 17 | +consider that your users will be interested to hear that your software offers |
| 18 | +even more value that it can be driven from a scripting language they already know. |
| 19 | + |
| 20 | +Python is widespread in finance, sciences, hobbyist programming and is often |
| 21 | +endearingly regarded as most popular programming language for non-developers. |
| 22 | +If your application can be driven by embedded Python, then chances are others will |
| 23 | +feel excited and empowered that your project can be used out of the box |
| 24 | +and feel like familiar territory. |
| 25 | + |
| 26 | +### But what about the lack of python modules? |
| 27 | + |
| 28 | +There are only be a small number of native modules available, but don't forget you have the entire |
| 29 | +Go standard library and *any* Go package you can name at your fingertips to expose! |
| 30 | +This plus multi-context capability gives gpython enormous potential on how it can |
| 31 | +serve you. |
| 32 | + |
| 33 | +So basically, gpython is only off the table if you need to run python that makes heavy use of |
| 34 | +modules that are only available in CPython. |
| 35 | + |
| 36 | +### Packing List |
| 37 | + |
| 38 | +| | | |
| 39 | +|---------------------- | ------------------------------------------------------------------| |
| 40 | +| `main.go` | if no args, runs in REPL mode, otherwise runs the given file | |
| 41 | +| `lib/mylib.py` | models a library that your application would expose for users | |
| 42 | +| `lib/REPL-startup.py` | invoked by `main.go` when starting REPL mode | |
| 43 | +| `mylib-demo.py` | models a user-authored script that consumes `mylib` | |
| 44 | +| `mylib.module.go` | Go implementation of `mylib_go` consumed by `mylib` | |
| 45 | + |
| 46 | + |
| 47 | +### Invoking a Python Script |
| 48 | + |
| 49 | +```bash |
| 50 | +$ cd examples/embedding/ |
| 51 | +$ go build . |
| 52 | +$ ./embedding mylib-demo.py |
| 53 | +``` |
| 54 | +``` |
| 55 | +Welcome to a gpython embedded example, |
| 56 | + where your wildest Go-based python dreams come true! |
| 57 | +
|
| 58 | +========================================================== |
| 59 | + Python 3.4 (github.com/go-python/gpython) |
| 60 | + go1.17.6 on darwin amd64 |
| 61 | +========================================================== |
| 62 | +
|
| 63 | +Spring Break itinerary: |
| 64 | + Stop 1: Miami, Florida | 7 nights |
| 65 | + Stop 2: Mallorca, Spain | 3 nights |
| 66 | + Stop 3: Ibiza, Spain | 14 nights |
| 67 | + Stop 4: Monaco | 12 nights |
| 68 | +### Made with Vacaton 1.0 by Fletch F. Fletcher |
| 69 | +
|
| 70 | +I bet Monaco will be the best! |
| 71 | +``` |
| 72 | + |
| 73 | +### REPL Mode |
| 74 | + |
| 75 | +```bash |
| 76 | +$ ./embedding |
| 77 | +``` |
| 78 | +``` |
| 79 | +======= Entering REPL mode, press Ctrl+D to exit ======= |
| 80 | +
|
| 81 | +========================================================== |
| 82 | + Python 3.4 (github.com/go-python/gpython) |
| 83 | + go1.17.6 on darwin amd64 |
| 84 | +========================================================== |
| 85 | +
|
| 86 | +>>> v = Vacation("Spring Break", Stop("Florida", 3), Stop("Nice", 7)) |
| 87 | +>>> print(str(v)) |
| 88 | +Spring Break, 2 stop(s) |
| 89 | +>>> v.PrintItinerary() |
| 90 | +Spring Break itinerary: |
| 91 | + Stop 1: Florida | 3 nights |
| 92 | + Stop 2: Nice | 7 nights |
| 93 | +### Made with Vacaton 1.0 by Fletch F. Fletcher |
| 94 | +``` |
| 95 | + |
| 96 | +## Takeways |
| 97 | + |
| 98 | + - `main.go` demonstrates high-level convenience functions such as `py.RunFile()`. |
| 99 | + - Embedding a Go `struct` as a Python object only requires that it implement `py.Object`, which is a single function: |
| 100 | + `Type() *py.Type` |
| 101 | + - See [py/run.go](https://github.com/go-python/gpython/tree/master/py/run.go) for more about interpreter instances and `py.Context` |
| 102 | + - Helper functions are available in [py/util.go](https://github.com/go-python/gpython/tree/master/py/util.go) and your contributions are welcome! |
0 commit comments