Skip to content

Python Support for Fable #2339

Closed
Closed
@dbrattli

Description

@dbrattli

Description

This issue is a discussion about Python support for Fable. A POC has been made to show that this is possible. However, quite a lot of work is needed to:

  1. Add decent support for Python
  2. Make Fable more JS agnostic and support multiple target languages.

Use cases:

  • F# running in Jupyter notebook with Python kernel, i.e similar to what is done with e.g Hy and Calysto Hy.
  • F# scripting using Python for reduced friction
  • More easily support embedded environments like the micro:bit and Rasberry PI.
  • Sharing data model between F# and Python code

Things to discuss and decide:

  • Should Python for F# be more Pythonic than .NET? There is already .NET support for Jupyter which we should not try to replace or compete with. Fable for Python should perhaps target Python instead of .NET (if possible) and be a more natural fit for Python developers looking to F#.
  • Should we try to be compatible with Babel AST or should we diverge into our own Python AST. This is perhaps inevitable, but Babel gives us a good starting point.
  • Should we for Fable (as with Peeble) or should we stay within Fable and have Fable support multiple languages. Having our own repo gives freedom and speed (by not needing to care about JS), but we will risk being left behind and obsoleted over time. My feeling is that we should become part of Fable.
  • Python basic types is also different from F#. E.g int has an arbitrary length. Should we emulate .NET (machine) types to be compatible or should we expose the Python int type as an F# int?

Installing the POC

The source code of the POC currently lives here:

  1. Install the latest Python 3.9 from https://www.python.org or brew install python@3.9 on Mac. Note that python may be available on your system as python, python3 or both.
  2. Clone both repo and switch to the python branch.
  3. Build fable-library for Python: dotnet fsi build.fsx library-py
  4. To run tests: dotnet fsi build.fsx test-py
  5. In Fable, edit QuickTest.fs to some simple F# code. Then run dotnet fsi build.fsx quicktest-py from the top directory.

Now you can edit both Fable and Expression code and the example code will recompile when needed. Note you need an additional save to QuickTest.fs to trigger recompile after modifying Fable code. The python file will be available as quicktest.py. A nice demo is to view both F#, JS, and Python in vscode at the same time. Then you will see how the code is being transformed when you save the F# file:

Screenshot 2021-01-15 at 13 18 09

You can run the generated Python code in the terminal:

$ python3 quicktest.py

Links

A few relevant links:

Feel free to contribute with discussions, comments, ideas, and code 😍

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions