Hello, I am trying to see how ScalaPy works through a simple example to visualize a Jupyter widget using the python module of ipywidgets, with the following code:
import $ivy.`me.shadaj::scalapy-core:0.4.2` import me.shadaj.scalapy.py import me.shadaj.scalapy.py.SeqConverters val w = py.module ("ipywidgets") w: py.Module = <module 'ipywidgets' from '/home/javier/anaconda3/envs/jupyterlab-env/lib/python3.8/site-packages/ipywidgets/__init__.py'> val dsp = py.module("IPython.display") dsp: py.Module = <module 'IPython.display' from '/home/javier/anaconda3/envs/jupyterlab-env/lib/python3.8/site-packages/IPython/display.py'> val w1 = w.IntSlider(10) w1: py.Dynamic = IntSlider(value=10) dsp.display(w1.toPythonProxy)
But I don't know how to pass the reference of the python IntSlider object to the python display function. I have tried with the toPythonProxy function but it is not defined for the IntSlider widget object and it produces an error:
me.shadaj.scalapy.py.PythonException: <class 'AttributeError'> 'IntSlider' object has no attribute 'toPythonProxy' me.shadaj.scalapy.interpreter.CPythonInterpreter$.$anonfun$throwErrorIfOccured$1(CPythonInterpreter.scala:315) me.shadaj.scalapy.interpreter.Platform$.Zone(Platform.scala:10) me.shadaj.scalapy.interpreter.CPythonInterpreter$.throwErrorIfOccured(CPythonInterpreter.scala:301) me.shadaj.scalapy.interpreter.CPythonInterpreter$.$anonfun$select$1(CPythonInterpreter.scala:501) me.shadaj.scalapy.interpreter.CPythonInterpreter$.withGil(CPythonInterpreter.scala:148) me.shadaj.scalapy.interpreter.CPythonInterpreter$.select(CPythonInterpreter.scala:493) me.shadaj.scalapy.py.AnyDynamics.selectDynamic(Dynamic.scala:43) me.shadaj.scalapy.py.AnyDynamics.selectDynamic$(Dynamic.scala:42) me.shadaj.scalapy.py.AnyDynamics$$anon$7$$anon$8.selectDynamic(Dynamic.scala:31) ammonite.$sess.cmd9$Helper.<init>(cmd9.sc:1) ammonite.$sess.cmd9$.<clinit>(cmd9.sc:7)
How could I pass the IntSlider (py.Dynamic) object to the python display function?
res11: py.Dynamic = None
toPythonCopyonly need to be used when passing Scala sequences into Python code (an explicit conversion is required because these operations can be fairly costly on the JVM).
python3fail to load, since ScalaPy has logic (in 0.4.2) to fall back to other Python versions (https://github.com/shadaj/scalapy/blob/master/core/jvm/src/main/scala/me/shadaj/scalapy/interpreter/CPythonAPI.scala#L10). Can you ensure that you're on the latest version? This behavior used to be buggy in previous versions.
It seems to me that I had a similar problem to yours (during running the scalaPy in intellij), and in my case, it was solved in the next way:
1. firstly run python3-config --ldflags
You maybe will see something like this: -L/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/config-3.9-darwin -ldl -framework CoreFoundation
and check if the libpython3.9.dylib exists
Maybe it will be helpful
brew install python3to install python. Maybe you can try to install python with homebrew but it will possibly have conflicts with anaconda's python (I am not completely sure in i), so maybe it is better to uninstall it before installing python with homebrew
@ekrich @jlareckm_twitter and @shadaj:matrix.org : I figured it out! @jlareckm_twitter your thread was a great help, thanks a lot! I found out thanks to your image where to change the VM options and even where to put the environmental variable, and it worked in my IntelliJ version! Thanks a ton!!!
@shadaj:matrix.org and anyone out there with an Anaconda install: it might be just me, but the 'python3-config --ldflags' approach does not point to the right folder on the latest version of Anaconda Python on a Mac. It does point to the correct Python version in the Darwin folder, but this contains what I think is a symbolic link to the library (libpython3.8.a). It does not work with Scalapy. The trick that worked for me was to redict the path manually as a VM option straight to '/Applications/anaconda3/lib' where the correct 'libpython3.8.dylib' exists.
Is it me and my particular Python install, or does anyone else have this issue too? And I hate to ask it, but how do I set this up on Almond, which was my initial idea?
Thanks and sorry for being a Gitter pain! :)
So, I am playing around on Almond with Scala 13.5 and Python 3.8. I encountered an issue with matplotlib. I first do this import:
val plt = py.module("matplotlib.pyplot")
The I execute:
But my JVM crashes and I get this error:
"Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'NSWindow drag regions should only be invalidated on the Main Thread!"
Anyone else has this issue? Can we not use libraries with GUI outputs, or maybe we need to do something in addition to that?
Modulein parallel. My module uses CPython libraries like pandas and NumPy. I was reusing the module instance and using the
applyDynamicmethod in each iteration. I noticed that there are over 10,000 files opened by the process. Is it expected behavior?