Skip to content

Commit 3bb48e8

Browse files
authored
Make examples into fenced code blocks (#1100)
1 parent d46bf35 commit 3bb48e8

File tree

1 file changed

+80
-61
lines changed

1 file changed

+80
-61
lines changed

README.md

Lines changed: 80 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,12 @@ can change the Python version by setting the `PYTHON` environment variable
5252
to the path of the `python` (or `python3` etc.) executable and then re-running `Pkg.build("PyCall")`.
5353
In Julia:
5454

55-
ENV["PYTHON"] = "... path of the python executable ..."
56-
# ENV["PYTHON"] = raw"C:\Python310-x64\python.exe" # example for Windows, "raw" to not have to escape: "C:\\Python310-x64\\python.exe"
57-
58-
# ENV["PYTHON"] = "/usr/bin/python3.10" # example for *nix
59-
Pkg.build("PyCall")
55+
```julia
56+
ENV["PYTHON"] = "... path of the python executable ..."
57+
# ENV["PYTHON"] = raw"C:\Python310-x64\python.exe" # example for Windows, "raw" to not have to escape: "C:\\Python310-x64\\python.exe"
58+
# ENV["PYTHON"] = "/usr/bin/python3.10" # example for *nix
59+
Pkg.build("PyCall")
60+
```
6061

6162
Note also that you will need to re-run `Pkg.build("PyCall")` if your
6263
`python` program changes significantly (e.g. you switch to a new
@@ -102,9 +103,11 @@ Windows, or using the Julia Conda package, in order to minimize headaches.
102103

103104
Here is a simple example to call Python's `math.sin` function:
104105

105-
using PyCall
106-
math = pyimport("math")
107-
math.sin(math.pi / 4) # returns ≈ 1/√2 = 0.70710678...
106+
```julia
107+
using PyCall
108+
math = pyimport("math")
109+
math.sin(math.pi / 4) # returns ≈ 1/√2 = 0.70710678...
110+
```
108111

109112
Type conversions are automatically performed for numeric, boolean,
110113
string, IO stream, date/period, and function types, along with tuples,
@@ -122,10 +125,13 @@ Keyword arguments can also be passed. For example, matplotlib's
122125
[pyplot](https://matplotlib.org/) uses keyword arguments to specify plot
123126
options, and this functionality is accessed from Julia by:
124127

125-
plt = pyimport("matplotlib.pyplot")
126-
x = range(0;stop=2*pi,length=1000); y = sin.(3*x + 4*cos.(2*x));
127-
plt.plot(x, y, color="red", linewidth=2.0, linestyle="--")
128-
plt.show()
128+
```julia
129+
plt = pyimport("matplotlib.pyplot")
130+
x = range(0;stop=2*pi,length=1000)
131+
y = sin.(3*x + 4*cos.(2*x))
132+
plt.plot(x, y, color="red", linewidth=2.0, linestyle="--")
133+
plt.show()
134+
```
129135

130136
However, for better integration with Julia graphics backends and to
131137
avoid the need for the `show` function, we recommend using matplotlib
@@ -135,14 +141,18 @@ Arbitrary Julia functions can be passed to Python routines taking
135141
function arguments. For example, to find the root of cos(x) - x,
136142
we could call the Newton solver in scipy.optimize via:
137143

138-
so = pyimport("scipy.optimize")
139-
so.newton(x -> cos(x) - x, 1)
144+
```julia
145+
so = pyimport("scipy.optimize")
146+
so.newton(x -> cos(x) - x, 1)
147+
```
140148

141149
A macro exists for mimicking Python's "with statement". For example:
142150

143-
@pywith pybuiltin("open")("file.txt","w") as f begin
144-
f.write("hello")
145-
end
151+
```julia
152+
@pywith pybuiltin("open")("file.txt","w") as f begin
153+
f.write("hello")
154+
end
155+
```
146156

147157
The type of `f` can be specified with `f::T` (for example, to override automatic
148158
conversion, use `f::PyObject`). Similarly, if the context manager returns a type
@@ -430,66 +440,76 @@ documentation `?pyfunction` and `?pyfunctionret` for more information.
430440
`@pydef` creates a Python class whose methods are implemented in Julia.
431441
For instance,
432442

433-
P = pyimport("numpy.polynomial")
434-
@pydef mutable struct Doubler <: P.Polynomial
435-
function __init__(self, x=10)
436-
self.x = x
437-
end
438-
my_method(self, arg1::Number) = arg1 + 20
439-
x2.get(self) = self.x * 2
440-
function x2.set!(self, new_val)
441-
self.x = new_val / 2
442-
end
443+
```julia
444+
P = pyimport("numpy.polynomial")
445+
@pydef mutable struct Doubler <: P.Polynomial
446+
function __init__(self, x=10)
447+
self.x = x
448+
end
449+
my_method(self, arg1::Number) = arg1 + 20
450+
x2.get(self) = self.x * 2
451+
function x2.set!(self, new_val)
452+
self.x = new_val / 2
443453
end
444-
Doubler().x2
454+
end
455+
Doubler().x2
456+
```
445457

446458
is essentially equivalent to the following Python code:
447459

448-
import numpy.polynomial
449-
class Doubler(numpy.polynomial.Polynomial):
450-
def __init__(self, x=10):
451-
self.x = x
452-
def my_method(self, arg1): return arg1 + 20
453-
@property
454-
def x2(self): return self.x * 2
455-
@x2.setter
456-
def x2(self, new_val):
457-
self.x = new_val / 2
458-
Doubler().x2
460+
```python
461+
import numpy.polynomial
462+
class Doubler(numpy.polynomial.Polynomial):
463+
def __init__(self, x=10):
464+
self.x = x
465+
def my_method(self, arg1): return arg1 + 20
466+
@property
467+
def x2(self): return self.x * 2
468+
@x2.setter
469+
def x2(self, new_val):
470+
self.x = new_val / 2
471+
Doubler().x2
472+
```
459473

460474
The method arguments and return values are automatically converted between Julia and Python. All Python special methods are supported (`__len__`, `__add__`, etc.).
461475

462476
`@pydef` allows for multiple inheritance of Python classes:
463477

464-
@pydef mutable struct SomeType <: (BaseClass1, BaseClass2)
465-
...
466-
end
478+
```julia
479+
@pydef mutable struct SomeType <: (BaseClass1, BaseClass2)
480+
...
481+
end
482+
```
467483

468484
Here's another example using [Tkinter](https://wiki.python.org/moin/TkInter):
469485

470-
using PyCall
471-
tk = pyimport("Tkinter")
486+
```julia
487+
using PyCall
488+
tk = pyimport("Tkinter")
472489

473-
@pydef mutable struct SampleApp <: tk.Tk
474-
__init__(self, args...; kwargs...) = begin
475-
tk.Tk.__init__(self, args...; kwargs...)
476-
self.label = tk.Label(text="Hello, world!")
477-
self.label.pack(padx=10, pady=10)
478-
end
490+
@pydef mutable struct SampleApp <: tk.Tk
491+
__init__(self, args...; kwargs...) = begin
492+
tk.Tk.__init__(self, args...; kwargs...)
493+
self.label = tk.Label(text="Hello, world!")
494+
self.label.pack(padx=10, pady=10)
479495
end
496+
end
480497

481-
app = SampleApp()
482-
app.mainloop()
498+
app = SampleApp()
499+
app.mainloop()
500+
```
483501

484502
Class variables are also supported:
485503

486-
using PyCall
487-
@pydef mutable struct ObjectCounter
488-
obj_count = 0 # Class variable
489-
function __init__(::PyObject)
490-
ObjectCounter.obj_count += 1
491-
end
504+
```julia
505+
using PyCall
506+
@pydef mutable struct ObjectCounter
507+
obj_count = 0 # Class variable
508+
function __init__(::PyObject)
509+
ObjectCounter.obj_count += 1
492510
end
511+
end
512+
```
493513

494514
### GUI Event Loops
495515

@@ -499,8 +519,7 @@ convenient to start the GUI event loop (which processes things like
499519
mouse clicks) as an asynchronous task within Julia, so that the GUI is
500520
responsive without blocking Julia's input prompt. PyCall includes
501521
functions to implement these event loops for some of the most common
502-
cross-platform [GUI
503-
toolkits](https://en.wikipedia.org/wiki/Widget_toolkit):
522+
cross-platform [GUI toolkits](https://en.wikipedia.org/wiki/Widget_toolkit):
504523
[wxWidgets](http://www.wxwidgets.org/), [GTK+](http://www.gtk.org/)
505524
version 2 (via [PyGTK](http://www.pygtk.org/)) or version 3 (via
506525
[PyGObject](https://pygobject.readthedocs.io/en/latest/)), and
@@ -627,7 +646,7 @@ To use `PyCall` with a certain virtual environment, set the environment
627646
variable `PYCALL_JL_RUNTIME_PYTHON` *before* importing `PyCall` to
628647
path to the Python executable. For example:
629648

630-
```julia
649+
```julia-repl
631650
$ source PATH/TO/bin/activate # activate virtual environment in system shell
632651
633652
$ julia # start Julia

0 commit comments

Comments
 (0)