-
Notifications
You must be signed in to change notification settings - Fork 241
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #854 from alexarchambault/scalapy-rich-print-rebased
Add support for rich display of Python objects in ScalaPy (rebased)
- Loading branch information
Showing
8 changed files
with
349 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,248 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 1, | ||
"metadata": { | ||
|
||
}, | ||
"outputs": [ | ||
{ | ||
"data": { | ||
"text/plain": [ | ||
"\u001b[32mimport \u001b[39m\u001b[36m$ivy.$ \n", | ||
"\u001b[39m\n", | ||
"\u001b[32mimport \u001b[39m\u001b[36mai.kien.python.Python\n", | ||
"\n", | ||
"\u001b[39m" | ||
] | ||
}, | ||
"execution_count": 1, | ||
"metadata": { | ||
|
||
}, | ||
"output_type": "execute_result" | ||
} | ||
], | ||
"source": [ | ||
"import $ivy.`ai.kien::python-native-libs:0.2.3`\n", | ||
"import ai.kien.python.Python\n", | ||
"\n", | ||
"Python().scalapyProperties.fold(\n", | ||
" ex => throw new Exception(ex),\n", | ||
" props => props.map { kv => println(kv); kv }.foreach(Function.tupled(System.setProperty _))\n", | ||
")" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 2, | ||
"metadata": { | ||
|
||
}, | ||
"outputs": [ | ||
{ | ||
"data": { | ||
"text/plain": [ | ||
"\u001b[32mimport \u001b[39m\u001b[36m$ivy.$ \n", | ||
"\u001b[39m\n", | ||
"\u001b[32mimport \u001b[39m\u001b[36mme.shadaj.scalapy.py\n", | ||
"\u001b[39m\n", | ||
"\u001b[32mimport \u001b[39m\u001b[36mme.shadaj.scalapy.py.PyQuote\n", | ||
"\u001b[39m\n", | ||
"\u001b[32mimport \u001b[39m\u001b[36mme.shadaj.scalapy.py.SeqConverters\u001b[39m" | ||
] | ||
}, | ||
"execution_count": 2, | ||
"metadata": { | ||
|
||
}, | ||
"output_type": "execute_result" | ||
} | ||
], | ||
"source": [ | ||
"import $ivy.`me.shadaj::scalapy-core:0.5.2`\n", | ||
"import me.shadaj.scalapy.py\n", | ||
"import me.shadaj.scalapy.py.PyQuote\n", | ||
"import me.shadaj.scalapy.py.SeqConverters" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 3, | ||
"metadata": { | ||
|
||
}, | ||
"outputs": [ | ||
|
||
], | ||
"source": [ | ||
"almond.scalapy.initDisplay" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 4, | ||
"metadata": { | ||
|
||
}, | ||
"outputs": [ | ||
|
||
], | ||
"source": [ | ||
"// disable pprint so that the next line won't show any output\n", | ||
"repl.pprinter() = repl.pprinter().copy(defaultHeight = 0)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 5, | ||
"metadata": { | ||
|
||
}, | ||
"outputs": [ | ||
{ | ||
"data": { | ||
"text/plain": [ | ||
"......" | ||
] | ||
}, | ||
"execution_count": 5, | ||
"metadata": { | ||
|
||
}, | ||
"output_type": "execute_result" | ||
} | ||
], | ||
"source": [ | ||
"val display = py.module(\"IPython.display\")" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 6, | ||
"metadata": { | ||
|
||
}, | ||
"outputs": [ | ||
{ | ||
"data": { | ||
"text/html": [ | ||
"<b>hello</b>" | ||
] | ||
}, | ||
"metadata": { | ||
|
||
}, | ||
"output_type": "display_data" | ||
} | ||
], | ||
"source": [ | ||
"display.HTML(\"<b>hello</b>\")" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 7, | ||
"metadata": { | ||
|
||
}, | ||
"outputs": [ | ||
{ | ||
"data": { | ||
"text/latex": [ | ||
"\\begin{eqnarray}\n", | ||
"\\nabla \\times \\vec{\\mathbf{B}} -\\, \\frac1c\\, \\frac{\\partial\\vec{\\mathbf{E}}}{\\partial t} & = \\frac{4\\pi}{c}\\vec{\\mathbf{j}} \\\\\n", | ||
"\\nabla \\cdot \\vec{\\mathbf{E}} & = 4 \\pi \\rho \\\\\n", | ||
"\\nabla \\times \\vec{\\mathbf{E}}\\, +\\, \\frac1c\\, \\frac{\\partial\\vec{\\mathbf{B}}}{\\partial t} & = \\vec{\\mathbf{0}} \\\\\n", | ||
"\\nabla \\cdot \\vec{\\mathbf{B}} & = 0 \n", | ||
"\\end{eqnarray}" | ||
] | ||
}, | ||
"metadata": { | ||
|
||
}, | ||
"output_type": "display_data" | ||
} | ||
], | ||
"source": [ | ||
"display.Latex(\"\"\"\\begin{eqnarray}\n", | ||
"\\nabla \\times \\vec{\\mathbf{B}} -\\, \\frac1c\\, \\frac{\\partial\\vec{\\mathbf{E}}}{\\partial t} & = \\frac{4\\pi}{c}\\vec{\\mathbf{j}} \\\\\n", | ||
"\\nabla \\cdot \\vec{\\mathbf{E}} & = 4 \\pi \\rho \\\\\n", | ||
"\\nabla \\times \\vec{\\mathbf{E}}\\, +\\, \\frac1c\\, \\frac{\\partial\\vec{\\mathbf{B}}}{\\partial t} & = \\vec{\\mathbf{0}} \\\\\n", | ||
"\\nabla \\cdot \\vec{\\mathbf{B}} & = 0 \n", | ||
"\\end{eqnarray}\"\"\")\n" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 8, | ||
"metadata": { | ||
|
||
}, | ||
"outputs": [ | ||
{ | ||
"data": { | ||
"text/latex": [ | ||
"$\\displaystyle a = b + c$" | ||
] | ||
}, | ||
"metadata": { | ||
|
||
}, | ||
"output_type": "display_data" | ||
} | ||
], | ||
"source": [ | ||
"display.Math(\"a = b + c\")" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 9, | ||
"metadata": { | ||
|
||
}, | ||
"outputs": [ | ||
{ | ||
"data": { | ||
"text/markdown": [ | ||
"\n", | ||
"# title\n", | ||
"## subsec\n", | ||
"foo\n" | ||
] | ||
}, | ||
"metadata": { | ||
|
||
}, | ||
"output_type": "display_data" | ||
} | ||
], | ||
"source": [ | ||
"display.Markdown(\"\"\"\n", | ||
"# title\n", | ||
"## subsec\n", | ||
"foo\n", | ||
"\"\"\")" | ||
] | ||
} | ||
], | ||
"metadata": { | ||
"kernelspec": { | ||
"display_name": "Scala (sources)", | ||
"language": "scala", | ||
"name": "scala-debug" | ||
}, | ||
"language_info": { | ||
"codemirror_mode": "text/x-scala", | ||
"file_extension": ".sc", | ||
"mimetype": "text/x-scala", | ||
"name": "scala", | ||
"nbconvert_exporter": "script", | ||
"version": "2.12.12" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 4 | ||
} |
23 changes: 23 additions & 0 deletions
23
modules/scala/almond-scalapy/src/main/resources/format_display_data.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import json as __almond_scalapy_json | ||
|
||
|
||
def __almond_scalapy_format_display_data(obj, include): | ||
repr_methods = ((t, m) for m, t in include if m in set(dir(obj))) | ||
representations = ((t, getattr(obj, m)()) for t, m in repr_methods) | ||
|
||
display_data = ( | ||
(t, (r[0], r[1]) if isinstance(r, tuple) and len(r) == 2 else (r, None)) | ||
for t, r in representations if r is not None | ||
) | ||
display_data = [(t, m, md) for t, (m, md) in display_data if m is not None] | ||
|
||
data = [ | ||
(t, d if isinstance(d, str) else __almond_scalapy_json.dumps(d)) | ||
for t, d, _ in display_data | ||
] | ||
metadata = [ | ||
(t, md if isinstance(md, str) else __almond_scalapy_json.dumps(md)) | ||
for t, _, md in display_data if md is not None | ||
] | ||
|
||
return data, metadata |
48 changes: 48 additions & 0 deletions
48
modules/scala/almond-scalapy/src/main/scala/almond/scalapy/package.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package almond | ||
|
||
import java.{util => ju} | ||
import jupyter.{Displayer, Displayers} | ||
import me.shadaj.scalapy.interpreter.CPythonInterpreter | ||
import me.shadaj.scalapy.py | ||
import me.shadaj.scalapy.py.{PyQuote, SeqConverters} | ||
import scala.io.Source | ||
import scala.jdk.CollectionConverters._ | ||
|
||
package object scalapy { | ||
CPythonInterpreter.execManyLines(Source.fromResource("format_display_data.py").mkString) | ||
|
||
def initDisplay: Unit = { | ||
Displayers.register( | ||
classOf[py.Any], | ||
new Displayer[py.Any] { | ||
def display(obj: py.Any): ju.Map[String, String] = { | ||
val (data, _) = formatDisplayData(obj) | ||
if (data.isEmpty) null else data.asJava | ||
} | ||
} | ||
) | ||
} | ||
|
||
private val pyFormatDisplayData = py.Dynamic.global.__almond_scalapy_format_display_data | ||
|
||
private def formatDisplayData(obj: py.Any): (Map[String, String], Map[String, String]) = { | ||
val displayData = pyFormatDisplayData(obj, allReprMethods.toPythonCopy) | ||
val data = displayData.bracketAccess(0).as[List[(String, String)]].toMap | ||
val metadata = displayData.bracketAccess(1).as[List[(String, String)]].toMap | ||
|
||
(data, metadata) | ||
} | ||
|
||
private val mimetypes = Map( | ||
"svg" -> "image/svg+xml", | ||
"png" -> "image/png", | ||
"jpeg" -> "image/jpeg", | ||
"html" -> "text/html", | ||
"javascript" -> "application/javascript", | ||
"markdown" -> "text/markdown", | ||
"latex" -> "text/latex" | ||
) | ||
|
||
private lazy val allReprMethods: Seq[(String, String)] = | ||
mimetypes.map { case (k, v) => s"_repr_${k}_" -> v }.toSeq | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.