add an ihaskell-display module for displaying matplotlib visualizations

This commit is contained in:
Andrea Rossato 2025-03-08 18:29:08 +01:00
parent 7d4fec0141
commit 4eff1244c5
5 changed files with 1966 additions and 0 deletions

View File

@ -0,0 +1,44 @@
module IHaskell.Display.Matplotlib where
import qualified Data.ByteString.Char8 as Char
import qualified Data.ByteString.UTF8 as BSU
import qualified Data.Text.Encoding as T.Encoding
import System.IO.Temp
import System.FilePath ((</>))
import Graphics.Matplotlib
import IHaskell.Display
instance IHaskellDisplay Matplotlib where
display = graphDataDisplayBoth
-- Width and height
w, h :: Int
w = 300
h = 300
graphDataPNG :: Matplotlib -> IO DisplayData
graphDataPNG m = do
withSystemTempDirectory "ihaskell-matplotlib" $ \tmpdir -> do
let path = tmpdir </> "ihaskell-matplotlib.png"
-- Write the image.
res <- file path m
case res of
Left _ -> error "Matplotlib could not generate an immage"
Right _ -> do
-- Read back, and convert to base64.
imgData <- Char.readFile path
return $ png w h $ base64 imgData
graphDataSVG :: Matplotlib -> IO DisplayData
graphDataSVG m = do
res <- toSvg m
case res of
Left s -> error s
Right f -> return $ svg $ T.Encoding.decodeUtf8 $ BSU.fromString f
graphDataDisplayBoth :: Matplotlib -> IO Display
graphDataDisplayBoth fig = do
pngDisp <- graphDataPNG fig
svgDisp <- graphDataSVG fig
return $ Display [pngDisp, svgDisp]

View File

@ -0,0 +1,20 @@
The MIT License (MIT)
Copyright (c) 2025 Andrea Rossato andrea.rossato@unitn.it
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

File diff suppressed because one or more lines are too long

View File

@ -0,0 +1,3 @@
import Distribution.Simple
main = defaultMain

View File

@ -0,0 +1,69 @@
-- The name of the package.
name: ihaskell-matplotlib
-- The package version. See the Haskell package versioning policy (PVP)
-- for standards guiding when and how versions should be incremented.
-- http://www.haskell.org/haskellwiki/Package_versioning_policy
-- PVP summary: +-+------- breaking API changes
-- | | +----- non-breaking API additions
-- | | | +--- code changes with no API change
version: 0.1.0.1
-- A short (one-line) description of the package.
synopsis: IHaskell display instance for matplotlib (from matplotlib package)
-- A longer description of the package.
-- description:
-- URL for the project homepage or repository.
homepage: http://www.github.com/gibiansky/ihaskell
-- The license under which the package is released.
license: MIT
-- The file containing the license text.
license-file: LICENSE
-- The package author(s).
author: Andrea Rossatoe <andrea.rossato@unitn.it>
-- An email address to which users can send suggestions, bug reports, and
-- patches.
maintainer: Andrea Rossato <andrea.rossato@unitn.it>,
Andrew Gibiansky <andrew.gibiansky@gmail.com>
-- A copyright notice.
-- copyright:
category: Development
build-type: Simple
-- Extra files to be distributed with the package, such as examples or a
-- README.
-- extra-source-files:
-- Constraint on the version of Cabal needed to build this package.
cabal-version: 1.16
library
-- Modules exported by the library.
exposed-modules: IHaskell.Display.Matplotlib
ghc-options: -O -Wall -Wno-orphans
-- Other library packages from which modules are imported.
build-depends: base >=4.9 && <5,
bytestring,
utf8-string,
text,
temporary,
filepath,
matplotlib,
ihaskell >= 0.6.2
-- Directories containing source files.
-- hs-source-dirs:
-- Base language which the package is written in.
default-language: Haskell2010