Compatibility Tools
Some packages require a little extra help to work nicely with PythonCall.
Some of these are "fixes" that are silently applied for you, and some are just extra functions to bridge a gap. We aim to keep these as minimal as possible.
Stdlib
Whenever a Python exception is displayed by Julia, sys.last_traceback
and friends are set. This allows the post-mortem debugger pdb.pm()
to work. Disable by setting PythonCall.CONFIG.auto_sys_last_traceback = false
.
Tabular data & Pandas
A pandas.DataFrame
can be wrapped in Julia as a PyPandasDataFrame
, providing a Tables.jl
-compatible interface.
Furthermore, any Python object which can be converted to a PyTable
(e.g. pandas.DataFrame
can be converted to PyPandasDataFrame
) satisfies the Tables.jl interface.
In the other direction, the following functions can be used to convert any Tables.jl
-compatible table to a Python table.
PythonCall.pytable
— Functionpytable(src, format=:pandas; ...)
Construct a Python table from the Tables.jl-compatible table src
.
The format
controls the type of the resulting table, and is one of:
:pandas
: Apandas.DataFrame
. Keyword arguments are passed to theDataFrame
constructor.:columns
: Adict
mapping column names to columns.:rows
: Alist
of rows, which arenamedtuple
s.:rowdicts
: Alist
of rows, which aredict
s.
MatPlotLib / PyPlot / Seaborn
MatPlotLib figures can be shown with Julia's display mechanism, like display(fig)
or display(mime, fig)
.
This means that if you return a figure from a Jupyter or Pluto notebook cell, it will be shown. You can call display(plt.gcf())
to display the current figure.
We also provide a simple MatPlotLib backend: mpl.use("module://juliacall.matplotlib")
. Now you can call plt.show()
to display the figure with Julia's display mechanism. You can specify the format like plt.show(format="png")
.
GUIs (including MatPlotLib)
Event loops
If for example you wish to use PyPlot in interactive mode (matplotlib.pyplot.ion()
) then activating the correct event loop will allow it to work.
PythonCall.event_loop_on
— Functionevent_loop_on(g::Symbol; interval=0.04, fix=false)
Activate an event loop for the GUI framework g
, so that the framework can run in the background of a Julia session.
The event loop runs every interval
seconds. If fix
is true and g
is a Qt framework, then fix_qt_plugin_path
is called.
Supported values of g
(and the Python module they relate to) are: :pyqt4
(PyQt4), :pyqt5
(PyQt5), :pyside
(PySide), :pyside2
(PySide2), :gtk
(gtk), :gtk3
(gi), :wx
(wx), :tkinter
(tkinter).
PythonCall.event_loop_off
— Functionevent_loop_off(g::Symbol)
Terminate the event loop g
if it is running.
Qt path fix
PythonCall.fix_qt_plugin_path
— Functionfix_qt_plugin_path()
Try to set the QT_PLUGIN_PATH
environment variable in Python, if not already set.
This fixes the problem that Qt does not know where to find its qt.conf
file, because it always looks relative to sys.executable
, which can be the Julia executable not the Python one when using this package.
If CONFIG.auto_fix_qt_plugin_path
is true, then this is run automatically before PyQt4
, PyQt5
, PySide
or PySide2
are imported.
IPython
If Python is running an IPython kernel, then:
- Currently disabled: Julia's
Base.stdout
is set to Python'ssys.stdout
. - A
PythonDisplay
andIPythonDisplay
are pushed onto Julia's display stack, so thatdisplay(x)
goes to IPython if possible.