Skip to content

Commit 5a65b25

Browse files
committed
Refactor ipython
1 parent d5c5b19 commit 5a65b25

File tree

9 files changed

+166
-9
lines changed

9 files changed

+166
-9
lines changed

README.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,14 @@ Python bindings for Fable. This library will eventually contain Python (stdlib)
44
bindings for Fable based on Python
55
[typeshed](https://github.com/python/typeshed).
66

7+
It will also contain type binding for many other libraries as well such
8+
as Flask, MicroBit and many more
9+
710
## Installation
811

9-
TBD
12+
```sh
13+
dotnet package add Fable.Python
14+
```
1015

1116
## Usage
1217

examples/flask/Flask.fsproj

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@
1212
</ItemGroup>
1313
<ItemGroup>
1414
<ProjectReference Include="../../../Fable/src/Fable.Core/Fable.Core.fsproj" />
15-
<ProjectReference Include="../../stdlib/Fable.Python.fsproj" />
16-
<ProjectReference Include="../../flask/Fable.Python.Flask.fsproj" />
15+
<ProjectReference Include="../../src/Fable.Python.fsproj" />
1716
</ItemGroup>
1817

1918
<Import Project="..\..\.paket\Paket.Restore.targets" />

paket.dependencies

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ source https://api.nuget.org/v3/index.json
33
storage: none
44
framework: net5.0, netstandard2.0, netstandard2.1
55

6-
nuget FSharp.Core
6+
nuget FSharp.Core >= 5
77
nuget Fable.Core.Experimental >= 4.0.0-alpha-002
88

99
group Test

src/Fable.Python.fsproj

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<Author>Dag Brattli</Author>
77
<Copyright>Dag Brattli</Copyright>
88
<PackageLicenseFile>LICENSE</PackageLicenseFile>
9-
<Version>0.6.0</Version>
9+
<Version>0.8.0</Version>
1010
<WarnOn>3390;$(WarnOn)</WarnOn>
1111
</PropertyGroup>
1212
<ItemGroup>
@@ -17,11 +17,12 @@
1717
<Compile Include="stdlib/TkInter.fs" />
1818
<Compile Include="stdlib/Queue.fs" />
1919

20-
<Compile Include="flask/Flask.fs" />
21-
2220
<Compile Include="cognite-sdk/CogniteSdk.fs" />
23-
21+
<Compile Include="flask/Flask.fs" />
22+
<Compile Include="jupyter/IPython.fs" />
23+
<Compile Include="jupyter/IPyWidgets.fs" />
2424
<Compile Include="microbit/MicroBit.fs" />
25+
<Compile Include="microbit/Speach.fs" />
2526
</ItemGroup>
2627
<ItemGroup>
2728
<Content Include="pyproject.toml; *.fsproj; **\*.fs; **\*.fsi" PackagePath="fable\" />

src/jupyter/IPyWidgets.fs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
module Fable.Python.IPyWidgets
2+
3+
open Fable.Core
4+
5+
6+
type IWidget =
7+
abstract close : unit -> unit
8+
9+
type IIntSlider =
10+
inherit IWidget
11+
12+
abstract value : int
13+
abstract min : int
14+
abstract max : int
15+
16+
type IFloatSlider =
17+
inherit IWidget
18+
19+
abstract value : float
20+
abstract min : float
21+
abstract max : float
22+
23+
type IExports =
24+
[<Emit("$0.interact($1, x=$2)")>]
25+
abstract interact<'T1, 'T2> : fn : ('T1 -> 'T2) * x: 'T1 -> 'T2
26+
[<Emit("$0.interact($1, x=$2, y=$3)")>]
27+
abstract interact<'T1, 'T2, 'T3> : fn : ('T1*'T2 -> 'T3) * x: 'T1 * y: 'T2 -> 'T3
28+
29+
abstract IntSlider : unit -> IIntSlider
30+
abstract IntSlider : value: int -> IIntSlider
31+
abstract IntSlider : value: int * min: int * max: int * step: int * description: string -> IIntSlider
32+
33+
abstract FloatSlider : unit -> IFloatSlider
34+
abstract FloatSlider : value: float -> IFloatSlider
35+
abstract FloatSlider : value: float * min: int * max: int * step: int * description: string -> IFloatSlider
36+
37+
[<ImportAll("ipywidgets")>]
38+
let widgets : IExports = nativeOnly

src/jupyter/IPython.fs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
module Fable.Python.IPython
2+
3+
open Fable.Core
4+
5+
[<Import("display", "IPython.display")>]
6+
let display<'T>(obj: 'T) : unit = nativeOnly

src/microbit/MicroBit.fs

Lines changed: 97 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,64 @@
1+
/// Micro:Bit stubs for Fable Python
12
module Fable.Python.MicroBit
23

34
open Fable.Core
45

5-
/// Micro:Bit stubs for Fable Python
6+
7+
[<Import("Image", "microbit")>]
8+
type Image (image: string) =
9+
/// Gets the number of columns in an image
10+
member _.width () : int = nativeOnly
11+
12+
/// Gets the number of rows in an image
13+
member _.height () : int = nativeOnly
14+
15+
/// Sets the brightness of a pixel at the given position Cannot be used on inbuilt images.
16+
member _.set_pixel(x: int, y: int, value: int) : unit = nativeOnly
17+
member _.get_pixel(x: int, y: int) : int = nativeOnly
18+
19+
member _.shift_left(n: int) : unit = nativeOnly
20+
member _.shift_right(n: int) : unit = nativeOnly
21+
member _.shift_up(n: int) : unit = nativeOnly
22+
member _.shift_down(n: int) : unit = nativeOnly
23+
24+
/// Return an exact copy of the image.
25+
member _.copy() : Image = nativeOnly
26+
/// Return a new image by inverting the brightness of the pixels in the source image.
27+
member _.invert() : Image = nativeOnly
28+
29+
/// Return a new image by inverting the brightness of the pixels in the
30+
/// source image. Cannot be used on inbuilt images.
31+
member _.fill(value: int) : Image = nativeOnly
32+
33+
static member HEART : Image = nativeOnly
34+
static member HEART_SMALL : Image = nativeOnly
35+
static member HAPPY : Image = nativeOnly
36+
static member SMILE : Image = nativeOnly
37+
static member SAD : Image = nativeOnly
38+
static member CONFUSED : Image = nativeOnly
39+
static member ANGRY : Image = nativeOnly
40+
static member ASLEEP : Image = nativeOnly
41+
static member SURPRISED : Image = nativeOnly
42+
static member SILLY : Image = nativeOnly
43+
static member FABULOUS : Image = nativeOnly
44+
static member MEH : Image = nativeOnly
45+
46+
static member YES : Image = nativeOnly
47+
static member NO : Image = nativeOnly
48+
49+
static member ALL_CLOCKS : Image array = nativeOnly
50+
static member ALL_ARROWS : Image array = nativeOnly
51+
652
type IDisplay =
753
/// Clear the display.
854
abstract clear : unit -> unit
955

56+
/// Shows the image on the display
57+
abstract show : image: Image array * delay: int -> unit
58+
abstract show : image: Image -> unit
59+
abstract show : letter: char -> unit
60+
abstract show : number: int -> unit
61+
1062
/// Scrolls value horizontally on the display. If value is an integer or
1163
/// float it is first converted to a string using str(). The delay
1264
/// parameter controls how fast the text is scrolling. If wait is True,
@@ -44,3 +96,47 @@ type IButton =
4496

4597
[<Import("button", "microbit")>]
4698
let button: IButton = nativeOnly
99+
100+
type ICompass =
101+
/// Starts the calibration process. An instructive message will be scrolled
102+
/// to the user after which they will need to rotate the device in order to
103+
/// draw a circle on the LED display.
104+
abstract calibrate : unit -> unit
105+
/// Gives the compass heading, calculated from the above readings, as an
106+
/// integer in the range from 0 to 360, representing the angle in degrees,
107+
/// clockwise, with north as 0.
108+
abstract heading : unit -> int
109+
110+
[<Import("compass", "microbit")>]
111+
let compass : ICompass = nativeOnly
112+
113+
type IAccelerometer =
114+
/// Get the acceleration measurement in the x axis, as a positive or
115+
/// negative integer, depending on the direction. The measurement is given
116+
/// in milli-g. By default the accelerometer is configured with a range of
117+
/// +/- 2g, and so this method will return within the range of +/- 2000mg.
118+
abstract get_x : unit -> int
119+
/// Get the acceleration measurement in the y axis, as a positive or
120+
/// negative integer, depending on the direction. The measurement is given
121+
/// in milli-g. By default the accelerometer is configured with a range of
122+
/// +/- 2g, and so this method will return within the range of +/- 2000mg.
123+
abstract get_y : unit -> int
124+
/// Get the acceleration measurement in the z axis, as a positive or
125+
/// negative integer, depending on the direction. The measurement is given
126+
/// in milli-g. By default the accelerometer is configured with a range of
127+
/// +/- 2g, and so this method will return within the range of +/- 2000mg.
128+
abstract get_z : unit -> int
129+
130+
131+
[<Import("accelerometer", "microbit")>]
132+
let accelerometer : IAccelerometer = nativeOnly
133+
134+
[<Import("sleep", "microbit")>]
135+
///Wait for n milliseconds. One second is 1000 milliseconds".
136+
let sleep(milliseconds: int) = nativeOnly
137+
138+
139+
[<Import("temperature", "microbit")>]
140+
///Wait for n milliseconds. One second is 1000 milliseconds".
141+
let temperature() = nativeOnly
142+

src/microbit/Speach.fs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
module Fable.Python.Speach
2+
3+
open Fable.Core
4+
5+
type ISpeach =
6+
abstract say : text : string -> unit
7+
8+
[<ImportAll("speach")>]
9+
let speach : ISpeach = nativeOnly

src/stdlib/Builtins.fs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,3 +125,6 @@ let builtins: IExports = nativeOnly
125125

126126
[<Emit("__name__")>]
127127
let __name__: string = nativeOnly
128+
129+
/// Python print function. Takes a single argument, so can be used with e.g string interpolation.
130+
let print = builtins.print

0 commit comments

Comments
 (0)