How to embed a Bokeh figure in a Dash app - bokeh

I would like to embed a Bokeh figure inside a Dash app, but I can't figure out how to render the Bokeh JSON in Dash.
The simplest solution would be to use Bokeh or Dash on its own. All our other apps at work use Dash (so Bokeh alone is undesirable), however, Dash/Plotly cannot generate the required figure (a raster image overlaid on a tiled map).
There is an old post on the Plotly community forum asking the same question, and the proposed solution was to use the dash-alternative-viz package to embed the Bokeh figure. Unfortunately, the package doesn't render Bokeh plots, even when copying the example usage.py script. This has been raised as an issue on the repo, but the developers have yet to respond.
Bokeh can supply JSON data that BokehJS can use to render a standalone figure (bokeh.embed.json_item()), and the docs give an example of how to serve a Bokeh figure to a Flask app:
#app.route('/plot')
def plot():
p = make_plot('petal_width', 'petal_length')
return json.dumps(json_item(p, "myplot"))
How can I do something similar in Dash?

dash-alternative-viz is a proof-of-concept Dash component library that provides Dash interfaces to other visualization libraries like Bokeh. Check out their github page to read more and get started:
https://github.com/plotly/dash-alternative-viz-demo

Related

Figure instead of InteractiveDynamics app for julia agents model

I am following the schelling.jl tutorial from the Agents.jl package (https://juliadynamics.github.io/Agents.jl/stable/examples/schelling/). Towards the end of the tutorial they create an interactive application. But when I run the script (include("schelling.jl")), I end up with figure instead of an interactive app (cannot click on the run, reset buttons since it appears to be a figure). Any ideas how I get it as an interactive app?
using InteractiveDynamics
figure, adf, mdf = abm_data_exploration(model, agent_step!, dummystep, parange; ac=groupcolor, am=groupmarker, as = 10, adata, alabels)
Note I am using vscode IDE
You need to specify the proper plotting backend, CairoMakie can output static vector graphics but it can't open interactive windows. You can use GLMakie or WGLMakie instead.
Package
Description
GLMakie.jl
GPU-powered, interactive 2D and 3D plotting in standalone GLFW.jl windows.
CairoMakie.jl
Cairo.jl based, non-interactive 2D backend for publication-quality vector graphics.
WGLMakie.jl
WebGL-based interactive 2D and 3D plotting that runs within browsers.
#see Mackie's Backends & Output

How to export static plots from Plotly in R using Orca?

I have successfully installed Orca using Method 4 on the github page. However, I am unsure how best to proceed to export a static plot I have made in Plotly.
I have tried calling Orca in my R script using the syntax shown on the github page, but it returns "Error: The orca command-line utility is required to use the 'orca()' function".
I would appreciate if someone is able to explain the best way to proceed. I am inexperienced with the command-line, and do not understand the syntax shown for the command-line on the previously linked github page.

How to generate vector tiles and how to display it in leaflet

My data consists of many elements and attibutes, so I can't send entire json to client-side because it's too slow reading and displaying data.
At this point I need to make tiles from data. I have worked with tippecanoe, and it's so good but it "only" generates *.mbtiles (vector data inside *.pbf), and Leaflet 1.0.2 can't work with this format, so I tried to extract into zoom folders z/x/y with mbutil, but it seems that resulted *.pbf tiles are not correct because I've tried with Mapbox gl js and doesn't work well.
So first question is:
someone know how can I generate *.pbf tiles correctly from geojson files? I've tried some of options showed here: awesome-vector-tiles
And someone know if there's some plugin for Leaflet 1.0.2 that can work with vector tiles in *.mbtiles, *.pbf or *.json?
I've tried with mapbox-gl-leaflet, vectorgrid and tangram.
Thank you very much for your help
You can serve Mapbox vector tiles from a Tippecanoe generated .mbtiles file using TileServer-GL. Using Docker makes this task even easier.
Assuming you have already generated a file called geo.mbtiles using Tippecanoe in your current directory:
docker run -it -v $(pwd):/data -p 8080:80 klokantech/tileserver-gl geo.mbtiles
This will spin up a server, and it will give you an endpoint that serves .pbf vector tiles. These can be rendered with Mapbox GL, Leaflet.VectorGrid, etc.
You can use tippecanoe with the -e option to generate *.pbf files in the proper directory structure. See https://github.com/mapbox/tippecanoe#output-tileset
https://github.com/tangrams/tangram
Tangram: WebGL Maps for Vector Data
you can display vector tiles using this JS library with leaflet
Thanks

Jupyter: how to make simple illustrations

I am learning to use Jupyter/IPython Notebook as an electronic notebook. Sometimes I need simple illustrations to go along with my calculations, e.g. arrows to represent vector quantities. That's the kind of illustration for which TikZ would be used if we were in Latex. Having tried the TikZ magic extension and failed, I wonder if there's a more native (Python) way to do this. I don't see Matplotlib as the right tool for this sort of thing (correct me if I'm wrong).
If you think TikZ magic is indeed the way to go and I should try to get it to work, then do say so. Thanks.
TikZ (prefered solution)
If you're already familiar with TikZ the respective magic is probably the best option. To use it, simply follow the installation instruction in this repo (pip install git+git://github.com/mkrphys/ipython-tikzmagic.git) and load the extension as shown in on the githib page with %load_ext tikzmagic.
I just tried with IPython 3.1 and it works fine. Of course you have to have pdflatex available.
Matplotlib
If you want to draw simple arrows matplotlib can be used as well and is, of course, more pythonic than TikZ. A really simple example based on this example could look like
import matplotlib.pyplot as plt
%matplotlib inline
plt.axis('off')
plt.arrow(0, 0, 0.5, 0.5, head_width=0.05, head_length=0.1, fc='k', ec='k');
For more technical plots with lots of arrows and dimensions, I totally agree with you that matplotlib is not be preferred.
Other alternatives
There is also an asymptote magic found here. I haven't tried this yet, though.
Finally, you could use svgs either written in the notebook (hints see this question, or using Inkscape or similar and embed the resulting svg-file via the from IPython.display import SVG.

How to save an interactive plot produced by ggvis?

I have produced a plot using the ggvis package in R. The plot is interactive which can be changed by moving around the slider. However when I save it, it is just a picture of current status without sliders.
I have seen people posting these interactive plots on the website, so I think there should be a way to save it first. How can I do this?
From the Rstudio page for ggvis:
Note: If you’re viewing the HTML version of this document generated with knitr, the examples will have their interactive features disabled. You’ll need to run the code in R to see and use the interactive controls.
From this and the whole page I gather that sharing the interactive graphics can be done using Shiny Apps and/or R Markdown documents. To deploy either of those to people who do not have R installed you need to a) use Shiny Apps IO; or b) install Shiny Server, which has a free version with very basic administration capabilities, and a paid version for confidential information in corporate environment (which I'm only mentioning in case you are not allowed to share your data).
Either way, read through the Shiny Tutorial, the
R Markdown Tutorial, and the combination.

Resources