@@ -39,40 +39,40 @@ See the [examples](demo).
3939
4040### Hello World: Plot a line
4141
42- ``` go
43- // demo/plot/plot.go
42+ <!-- embedme demo/plot/plot.go -->
4443
44+ ``` go
4545package main
4646
47- import gp " github.com/cpunion/go-python"
47+ import . " github.com/cpunion/go-python"
4848
4949func main () {
50- gp. Initialize ()
51- plt := gp. ImportModule (" matplotlib.pyplot" )
52- plt.Call (" plot" , gp. MakeTuple (5 , 10 ), gp. MakeTuple (10 , 15 ), gp. KwArgs {" color" : " red" })
50+ Initialize ()
51+ plt := ImportModule (" matplotlib.pyplot" )
52+ plt.Call (" plot" , MakeTuple (5 , 10 ), MakeTuple (10 , 15 ), KwArgs{" color" : " red" })
5353 plt.Call (" show" )
5454}
5555
5656```
5757
5858### Typed Python Objects
5959
60- ``` go
61- // demo/plot2/plot2.go
60+ <!-- embedme demo/plot2/plot2.go -->
6261
62+ ``` go
6363package main
6464
65- import gp " github.com/cpunion/go-python"
65+ import . " github.com/cpunion/go-python"
6666
6767type plt struct {
68- gp. Module
68+ Module
6969}
7070
7171func Plt () plt {
72- return plt{gp. ImportModule (" matplotlib.pyplot" )}
72+ return plt{ImportModule (" matplotlib.pyplot" )}
7373}
7474
75- func (m plt ) Plot (args ...any ) gp . Object {
75+ func (m plt ) Plot (args ...any ) Object {
7676 return m.Call (" plot" , args...)
7777}
7878
@@ -81,26 +81,26 @@ func (m plt) Show() {
8181}
8282
8383func main () {
84- gp. Initialize ()
85- defer gp. Finalize ()
84+ Initialize ()
85+ defer Finalize ()
8686 plt := Plt ()
87- plt.Plot ([]int {5 , 10 }, []int {10 , 15 }, gp. KwArgs {" color" : " red" })
87+ plt.Plot ([]int {5 , 10 }, []int {10 , 15 }, KwArgs{" color" : " red" })
8888 plt.Show ()
8989}
9090
9191```
9292
9393### Define Python Objects with Go
9494
95- ``` go
96- // demo/module/foo/foo.go
95+ <!-- embedme demo/module/foo/foo.go -->
9796
97+ ``` go
9898package foo
9999
100100import (
101101 " fmt"
102102
103- gp " github.com/cpunion/go-python"
103+ . " github.com/cpunion/go-python"
104104)
105105
106106type Point struct {
@@ -131,47 +131,47 @@ func Add(a, b int) int {
131131 return a + b
132132}
133133
134- func InitFooModule () gp . Module {
135- m := gp. CreateModule (" foo" )
134+ func InitFooModule () Module {
135+ m := CreateModule (" foo" )
136136 // Add the function to the module
137137 m.AddMethod (" add" , Add, " (a, b) -> float\n --\n\n Add two integers." )
138138 // Add the type to the module
139- gp .AddType [Point](m , (*Point).init , " Point" , " Point objects" )
139+ m .AddType (Point{} , (*Point).init , " Point" , " Point objects" )
140140 return m
141141}
142142
143143```
144144
145145Call foo module from Python and Go.
146146
147- ``` go
148- // demo/module/module.go
147+ <!-- embedme demo/module/module.go -->
149148
149+ ``` go
150150package main
151151
152152import (
153153 " fmt"
154154
155- gp " github.com/cpunion/go-python"
155+ . " github.com/cpunion/go-python"
156156 " github.com/cpunion/go-python/demo/module/foo"
157157)
158158
159159func main () {
160- gp. Initialize ()
161- defer gp. Finalize ()
160+ Initialize ()
161+ defer Finalize ()
162162 fooMod := foo.InitFooModule ()
163- gp. GetModuleDict ().SetString (" foo" , fooMod)
163+ GetModuleDict ().SetString (" foo" , fooMod)
164164
165165 Main1 (fooMod)
166166 Main2 ()
167167}
168168
169- func Main1 (fooMod gp . Module ) {
169+ func Main1 (fooMod Module ) {
170170 sum := fooMod.Call (" add" , 1 , 2 ).AsLong ()
171171 fmt.Printf (" Sum of 1 + 2: %d \n " , sum.Int64 ())
172172
173173 dict := fooMod.Dict ()
174- Point := dict.Get (gp. MakeStr (" Point" )).AsFunc ()
174+ Point := dict.Get (MakeStr (" Point" )).AsFunc ()
175175
176176 point := Point.Call (3 , 4 )
177177 fmt.Printf (" dir(point): %v \n " , point.Dir ())
@@ -191,7 +191,7 @@ func Main1(fooMod gp.Module) {
191191
192192func Main2 () {
193193 fmt.Printf (" =========== Main2 ==========\n " )
194- _ = gp. RunString (`
194+ _ = RunString (`
195195import foo
196196point = foo.Point(3, 4)
197197print("dir(point):", dir(point))
@@ -213,15 +213,15 @@ point.print()
213213
214214### Call gradio
215215
216- ``` go
217- // demo/gradio/gradio.go
216+ <!-- embedme demo/gradio/gradio.go -->
218217
218+ ``` go
219219package main
220220
221221import (
222222 " os"
223223
224- gp " github.com/cpunion/go-python"
224+ . " github.com/cpunion/go-python"
225225)
226226
227227/*
@@ -243,16 +243,16 @@ with gr.Blocks() as demo:
243243demo.launch()
244244*/
245245
246- var gr gp. Module
246+ var gr Module
247247
248- func UpdateExamples (country string ) gp . Object {
248+ func UpdateExamples (country string ) Object {
249249 println (" country:" , country)
250250 if country == " USA" {
251- return gr.Call (" Dataset" , gp. KwArgs {
251+ return gr.Call (" Dataset" , KwArgs{
252252 " samples" : [][]string {{" Chicago" }, {" Little Rock" }, {" San Francisco" }},
253253 })
254254 } else {
255- return gr.Call (" Dataset" , gp. KwArgs {
255+ return gr.Call (" Dataset" , KwArgs{
256256 " samples" : [][]string {{" Islamabad" }, {" Karachi" }, {" Lahore" }},
257257 })
258258 }
@@ -264,15 +264,15 @@ func main() {
264264 return
265265 }
266266
267- gp. Initialize ()
268- defer gp. Finalize ()
269- gr = gp. ImportModule (" gradio" )
270- fn := gp. CreateFunc (UpdateExamples,
267+ Initialize ()
268+ defer Finalize ()
269+ gr = ImportModule (" gradio" )
270+ fn := CreateFunc (" update_examples " , UpdateExamples,
271271 " (country, /)\n --\n\n Update examples based on country" )
272272 // Would be (in the future):
273- // fn := gp. FuncOf(UpdateExamples)
274- demo := gp. With (gr.Call (" Blocks" ), func (v gp. Object ) {
275- dropdown := gr.Call (" Dropdown" , gp. KwArgs {
273+ // fn := FuncOf(UpdateExamples)
274+ demo := With (gr.Call (" Blocks" ), func (v Object) {
275+ dropdown := gr.Call (" Dropdown" , KwArgs{
276276 " label" : " Country" ,
277277 " choices" : []string {" USA" , " Pakistan" },
278278 " value" : " USA" ,
0 commit comments