Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
Discussion options

As discussed with Igor Alshannikov (@alshan), here is an idea for a function that would allow interactive plotting in a default web browser (or any other). This would be interesting when you are not using an IDE that allows interactive plotting.

The function is very simple, it ggsave a temporary .html file and then open it in a web browser. It needs webbrowser and tempfile packages both included in The Python Standard Library.

from lets_plot import *
import webbrowser
import tempfile

def ggshow_html(p):
    """
    Shows a temporary interactive plot p in the default web browser. When using ggshow_html() for different plots,
    plots will appear in different tabs.
    This function can be tuned to call any other web browser installed (see example in the code).
    Eric Gayer 2022 - 2024
    
    Parameters
    ----------
    p : a Lets-Plot plot

    Returns
    -------
    Interactive plot in default web browser.

    """
    temp = tempfile.NamedTemporaryFile()
    try:
        ggsave(p, temp.name+'.html')
        
        ### Use your default web browser
        webbrowser.get().open('file://'+ temp.name +'.html', new=2)
        
        ### Use a different web browser, here Google Chrome on Mac OS
        #webbrowser.get('open -a /Applications/Google\ Chrome.app %s').open('file://'+ temp.name +'.html', new=2)
        
    finally:
        temp.close()

Example :

import numpy as np
from lets_plot import *

np.random.seed(12)
data = dict(
    cond=np.repeat(['A', 'B'], 200),
    rating=np.concatenate((np.random.normal(0, 1, 200), np.random.normal(1, 1.5, 200)))
    )

p= ggplot(data, aes(x='rating', fill='cond')) + ggsize(700, 300) + \
    geom_density(color='dark_green', alpha=.7) + scale_fill_brewer(type='seq') + \
    theme(panel_grid_major_x='blank')

ggshow_html(p)
You must be logged in to vote

Replies: 2 comments · 2 replies

Comment options

Hi Eric, thank you for sharing! We will try to incorporate this code in some form into lets-plot.

You must be logged in to vote
0 replies
Comment options

Hi Eric (@egayer), we've added this feature.
In a nutshell:
Instead of LetsPlot.setup_html() you will add LetsPlot.setup_show_ext().
After that, p.show() will open the plot in a browser window.

You must be logged in to vote
2 replies
@egayer
Comment options

HI Igor Alshannikov (@alshan), awesome ! This is very clever, I like the idea of setting the web browser of your choice at anytime.

I'll install the 4.3 and test it right away !

@egayer
Comment options

It works very well ! thanks you !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
💡
Ideas
Labels
None yet
2 participants
Morty Proxy This is a proxified and sanitized view of the page, visit original site.