1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292
//! Plot data with Plots.jl and PyPlot.jl
//!
//! In order to use this module PyPlot.jl must have been installed, as well as GTK3. GTK3 is
//! currently the only supported GUI. You must use a `matplotlibrc` file that sets the backend
//! to `Gtk3Agg`.
//!
//! When multiple figures are open, only the most recently opened one is updated automatically.
#[cfg(feature = "async")]
use jlrs_macros::julia_version;
use crate::{
args::Values,
call::{Call, ProvideKeywords},
convert::into_jlrs_result::IntoJlrsResult,
data::managed::{
erase_scope_lifetime, function::Function, module::Module, value::Value, Managed,
},
error::JlrsResult,
memory::target::{frame::GcFrame, Target},
private::Private,
};
#[cfg(feature = "async")]
use crate::{call::CallAsync, memory::target::frame::AsyncGcFrame};
init_fn!(init_jlrs_py_plot, JLRS_PY_PLOT_JL, "JlrsPyPlot.jl");
/// A handle to a plotting window.
#[derive(Clone, Copy, Debug)]
#[repr(transparent)]
pub struct PyPlot<'scope>(Value<'scope, 'static>);
impl<'scope> PyPlot<'scope> {
/// This metod must be called before this module can be used.
pub fn init<'frame>(frame: &mut GcFrame<'frame>) {
if Module::main(&frame).submodule(&frame, "JlrsPyPlot").is_ok() {
return;
}
unsafe { init_jlrs_py_plot(frame) };
}
/// Create a new plotting window by calling `plot_fn(args...)`. The window stays open until it
/// has been closed, even if all handles have been dropped. `plot_fn` must be a plotting
/// function from the Plots.jl package, such as `plot` or `hexbin`. The resources associated
/// with the window are only cleaned up if one of the `PyPlot::wait` methods is called.
pub unsafe fn new<'value, V, const N: usize>(
frame: &mut GcFrame<'scope>,
plot_fn: Function<'value, 'static>,
args: V,
) -> JlrsResult<Self>
where
V: Values<'value, 'static, N>,
{
let values = args.into_extended_with_start([plot_fn.as_value()], Private);
let plt = Module::main(&frame)
.submodule(&frame, "JlrsPyPlot")
.unwrap()
.as_managed()
.function(&frame, "jlrsplot")
.unwrap()
.as_managed()
.call(frame, values.as_ref())
.into_jlrs_result()?;
Ok(PyPlot(plt))
}
/// Create a new plotting window by calling `plotfn(args...; keywords)`. The window stays open
/// until it has been closed, even if all handles have been dropped. `plot_fn` must be a
/// plotting function from the Plots.jl package, such as `plot` or `hexbin`. The resources
/// associated with the window are only cleaned up if one of the `PyPlot::wait` methods is
/// called.
pub unsafe fn new_with_keywords<'value, V, const N: usize>(
frame: &mut GcFrame<'scope>,
plot_fn: Function<'value, 'static>,
args: V,
keywords: Value<'value, 'static>,
) -> JlrsResult<Self>
where
V: Values<'value, 'static, N>,
{
let values = args.into_extended_with_start([plot_fn.as_value()], Private);
let plt = Module::main(&frame)
.submodule(&frame, "JlrsPyPlot")
.unwrap()
.as_managed()
.function(&frame, "jlrsplot")
.unwrap()
.as_managed()
.provide_keywords(keywords)?
.call(frame, values.as_ref())
.into_jlrs_result()?;
Ok(PyPlot(plt))
}
/// Update an existing plotting window by calling
/// `plot)fn(<plot associated with self>, args...)`. If the window has already been closed an
/// error is returned. Note that if multiple plotting windows are currently open, only the
/// most recently created one is redrawn automatically.
pub unsafe fn update<'value, 'frame, V, const N: usize>(
self,
frame: &mut GcFrame<'scope>,
plot_fn: Function<'value, 'static>,
args: V,
) -> JlrsResult<isize>
where
V: Values<'value, 'static, N>,
{
let values = args.into_extended_with_start([plot_fn.as_value()], Private);
Module::main(&frame)
.submodule(&frame, "JlrsPyPlot")
.unwrap()
.as_managed()
.function(&frame, "updateplot!")
.unwrap()
.as_managed()
.call(frame, values.as_ref())
.into_jlrs_result()?
.unbox::<isize>()
}
/// Update an existing plotting window by calling
/// `plot_fn(<plot associated with self>, args...; kwargs...)`. If the window has already been
/// closed an error is returned. Note that if multiple plotting windows are currently open,
/// only the most recently created one is redrawn automatically.
pub unsafe fn update_with_keywords<'value, 'frame, V, const N: usize>(
self,
frame: &mut GcFrame<'scope>,
plot_fn: Function<'value, 'static>,
args: V,
keywords: Value<'value, 'static>,
) -> JlrsResult<isize>
where
V: Values<'value, 'static, N>,
{
let values = args
.into_extended_with_start([erase_scope_lifetime(self.0), plot_fn.as_value()], Private);
Module::main(&frame)
.submodule(&frame, "JlrsPyPlot")
.unwrap()
.as_managed()
.function(&frame, "updateplot!")
.unwrap()
.as_managed()
.provide_keywords(keywords)?
.call(frame, values.as_ref())
.into_jlrs_result()?
.unbox::<isize>()
}
/// Wait until the window associated with `self` has been closed.
pub fn wait<'frame>(self, frame: &mut GcFrame<'scope>) -> JlrsResult<()> {
unsafe {
Module::base(&frame)
.function(&frame, "wait")?
.as_managed()
.call1(frame, self.0)
.into_jlrs_result()?;
Ok(())
}
}
/// Whenever a plot is updated with a non-mutating plotting function a new version is
/// created. Because all versions are protected from garbage collection until [`PyPlot::wait`]
/// has returned, it's possible to change the pending version which will be used as the base
/// plot when [`PyPlot::update`] is called.
pub fn set_pending_version<'frame>(
self,
frame: &mut GcFrame<'frame>,
version: isize,
) -> JlrsResult<()> {
frame.scope(|mut frame| unsafe {
let version = Value::new(&mut frame, version);
Module::main(&frame)
.submodule(&frame, "JlrsPyPlot")
.unwrap()
.as_managed()
.function(&frame, "setversion")
.unwrap()
.as_managed()
.call1(frame, version)
.into_jlrs_result()?;
Ok(())
})
}
/// Wait until the window associated with `self` has been closed in a new task scheduled
/// on the main thread.
#[cfg(feature = "async")]
pub async fn wait_async_main<'frame>(self, frame: &mut AsyncGcFrame<'frame>) -> JlrsResult<()> {
unsafe {
Module::base(&frame)
.function(&frame, "wait")?
.as_managed()
.call_async_main(frame, [self.0])
.await
.into_jlrs_result()?;
Ok(())
}
}
#[cfg(feature = "async")]
#[julia_version(since = "1.9")]
/// Wait until the window associated with `self` has been closed in a new task scheduled
/// on the `:interactive` thread pool.
pub async fn wait_async_interactive<'frame>(
self,
frame: &mut AsyncGcFrame<'frame>,
) -> JlrsResult<()> {
unsafe {
Module::base(&frame)
.function(&frame, "wait")?
.as_managed()
.call_async_interactive(frame, [self.0])
.await
.into_jlrs_result()?;
Ok(())
}
}
/// Wait until the window associated with `self` has been closed in a new task scheduled
/// on another thread.
#[cfg(feature = "async")]
pub async fn wait_async_local<'frame>(
self,
frame: &mut AsyncGcFrame<'frame>,
) -> JlrsResult<()> {
unsafe {
Module::base(&frame)
.function(&frame, "wait")?
.as_managed()
.call_async_local(frame, [self.0])
.await
.into_jlrs_result()?;
Ok(())
}
}
/// Wait until the window associated with `self` has been closed in a new task scheduled
/// on another thread.
#[cfg(feature = "async")]
pub async fn wait_async<'frame>(self, frame: &mut AsyncGcFrame<'frame>) -> JlrsResult<()> {
unsafe {
Module::base(&frame)
.function(&frame, "wait")?
.as_managed()
.call_async(frame, [self.0])
.await
.into_jlrs_result()?;
Ok(())
}
}
}
/// This trait is, and can only be, implemented by [`Module`]. It adds the method `Module::plots`
/// that provides access to the contents of the `Plots` package.
pub trait AccessPlotsModule: private::AccessPlotsModulePriv {
/// Returns the `Plots` module.
fn plots<'global, T: Target<'global>>(target: &T) -> Module<'global> {
unsafe {
Module::main(target)
.submodule(target, "JlrsPyPlot")
.unwrap()
.as_managed()
.submodule(target, "Plots")
.unwrap()
.as_managed()
}
}
}
impl<'scope> AccessPlotsModule for Module<'scope> {}
mod private {
use crate::data::managed::module::Module;
pub trait AccessPlotsModulePriv {}
impl<'scope> AccessPlotsModulePriv for Module<'scope> {}
}