Skip to content

Commit 77d77d3

Browse files
authored
Document --python flag (#2574)
* Shorten title to fit without line breaks in the left-hand-side menu on the website * Add Python/ScalaPy docs * Add to guides/intro.md
1 parent 5d95b1e commit 77d77d3

File tree

3 files changed

+111
-2
lines changed

3 files changed

+111
-2
lines changed

website/docs/guides/intro.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,5 @@ Less introductory guides on specific topics.
4848
- [SBT and Mill export](./sbt-mill.md) - learn how to convert your Scala CLI project into an SBT or Mill project (when
4949
you need a more powerful build tool).
5050
- [proxies](./repositories.md) - learn how to configure Scala CLI to work with proxies.
51-
- [Markdown](./markdown.md) - learn how to work with `.md` sources.
51+
- [Markdown](./markdown.md) - learn how to work with `.md` sources.
52+
- [Python/ScalaPy](./python.md) - learn how to use Python libraries in Scala CLI projects.

website/docs/guides/python.md

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
---
2+
title: Python/ScalaPy ⚡️
3+
sidebar_position: 70
4+
---
5+
6+
:::caution
7+
ScalaPy support is an experimental feature.
8+
9+
Please bear in mind that non-ideal user experience should be expected.
10+
If you encounter any bugs or have feedback to share, make sure to reach out to the maintenance team
11+
on [GitHub](https://github.com/VirtusLab/scala-cli).
12+
:::
13+
14+
ScalaPy is a library that allows you to access the Python interpreter from Scala code. It boasts a simple API, automatic conversion between Scala and Python types, and optional static typing.
15+
It makes it possible to integrate Python libraries into Scala CLI projects.
16+
17+
Scala CLI allows to configure the ScalaPy library with the `--python` flag and `//> using python` directive.
18+
19+
More information about ScalaPy can be found [here](https://scalapy.dev).
20+
21+
## Example usage
22+
23+
Some configuration might be needed before running the examples below:
24+
25+
```bash ignore
26+
# install Python 3.11 (e.g. via an installer from the official Python website)
27+
# then download the packages with
28+
pip3 install numpy matplotlib python-config
29+
```
30+
31+
```scala
32+
//> using python
33+
//> using scala 2.13
34+
35+
import me.shadaj.scalapy.py
36+
import me.shadaj.scalapy.py.SeqConverters
37+
import py.PyQuote
38+
39+
py.local {
40+
val np = py.module("numpy")
41+
42+
val rng = np.random.default_rng()
43+
44+
val randoms = rng.standard_normal(10).as[Seq[Double]]
45+
46+
randoms.foreach(println(_))
47+
}
48+
49+
val numbers = py"[x * 2 for x in ${Iterator.from(3).take(10).toList.toPythonCopy}]"
50+
.as[Seq[Int]]
51+
52+
println(numbers)
53+
```
54+
55+
You can also use Scala Native to create a native binary with direct bindings to CPython.
56+
57+
```scala
58+
//> using python
59+
60+
import me.shadaj.scalapy.py
61+
import me.shadaj.scalapy.py.SeqConverters
62+
63+
import scala.util.Random
64+
import scala.math.{Pi, sin, random}
65+
66+
object PlotDemo {
67+
@main
68+
def plot = {
69+
val sequences = generate3DataSeqs
70+
71+
py.local {
72+
val plt = py.module("matplotlib.pyplot")
73+
74+
for {
75+
(seq, color) <- sequences.zip(Seq("b", "r", "g"))
76+
} {
77+
plt.plot(seq.toPythonProxy, color = color)
78+
plt.show()
79+
}
80+
}
81+
}
82+
83+
def generate3DataSeqs: Seq[Seq[Double]] = {
84+
val amplitude = 1.0 // Amplitude of the sine wave
85+
val numSamples = 1000
86+
val numSequences = 3
87+
val noiseAmplitude = 0.2 // Amplitude of noise
88+
89+
// Generate three sequences with varying numbers of cycles
90+
val sequences = (1 to numSequences).map { seqIdx =>
91+
val frequency = seqIdx // Varying frequency for each sequence
92+
(1 to numSamples).map { sampleIdx =>
93+
val noise = (random * 2 - 1) * noiseAmplitude // Generate random noise
94+
val phase = 2 * Pi * frequency * sampleIdx / numSamples
95+
amplitude * sin(phase) + noise
96+
}
97+
}
98+
sequences
99+
}
100+
}
101+
102+
```
103+
Run:
104+
```bash ignore
105+
scala-cli --power package --native PlotDemo.scala -o plot
106+
./plot
107+
```
108+

website/docs/guides/repositories.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
title: Repositories and HTTP Proxies ⚡️
2+
title: Repositories and HTTP Proxy ⚡️
33
sidebar_position: 51
44
---
55

0 commit comments

Comments
 (0)