haskellthreepenny-gui

How to reload updated file in Threepenny-gui 0.6?


The Threepenny-gui changelog (https://hackage.haskell.org/package/threepenny-gui-0.6.0.1/changelog) reads: "The functions loadFile and loadDirectory have been removed, as I felt that the jsStatic option is sufficient for most use cases."

My question is: how can we reload an image that is updated during execution without loadFile?

With Threepenny-gui 0.5 I used the following code:

redraw :: UI.Element -> IORef CompTree -> (Maybe Vertex) -> UI ()
redraw img treeRef mcv
  = do tree <- UI.liftIO $ readIORef treeRef
       UI.liftIO $ writeFile ".Hoed/debugTree.dot" (shw $ summarize tree mcv)
       UI.liftIO $ system $ "dot -Tpng -Gsize=9,5 -Gdpi=100 .Hoed/debugTree.dot "
                          ++ "> .Hoed/wwwroot/debugTree.png"
       url <- UI.loadFile "image/png" ".Hoed/wwwroot/debugTree.png"
       UI.element img # UI.set UI.src url

When, with Threepenny-gui 0.6, I set jsStatic to Just "./.Hoed/wwwroot", the following code (obviously) results in my GUI only showing the initial image that already was there when my application started:

redraw :: UI.Element -> IORef CompTree -> (Maybe Vertex) -> UI ()
redraw img treeRef mcv
  = do tree <- UI.liftIO $ readIORef treeRef
       UI.liftIO $ writeFile ".Hoed/debugTree.dot" (shw $ summarize tree mcv)
       UI.liftIO $ system $ "dot -Tpng -Gsize=9,5 -Gdpi=100 .Hoed/debugTree.dot "
                          ++ "> .Hoed/wwwroot/debugTree.png"
       UI.element img # UI.set UI.src "static/debugTree.png"
       return ()

My full code for Threepenny-gui 0.5 is here: https://github.com/MaartenFaddegon/Hoed/blob/master/Debug/Hoed/DemoGUI.hs


Solution

  • (Author here.) Apparently, I didn't consider your use case when removing these functions. :-) I can add them back in if you like, could make an issue on github?

    There are various methods on the JavaScript side to reload a file at a certain URL. See for instance the question "Refresh image with a new one at the same url".