r""" Write a plotly scatter plot to HTML file that doesn't require server

    >>> from nlpia.loaders import get_data
    >>> df = get_data('etpinard')  # pd.read_csv('https://plot.ly/~etpinard/191.csv')
    >>> df.columns = [eval(c) if c[0] in '"\'' else str(c) for c in df.columns]
    >>> data = {'data': [
    ...          Scatter(x=df[continent+', x'],
    ...                  y=df[continent+', y'],
    ...                  text=df[continent+', text'],
    ...                  marker=Marker(size=df[continent+', size'].fillna(10000), sizemode='area', sizeref=131868,),
    ...                  mode='markers',
    ...                  name=continent) for continent in ['Africa', 'Americas', 'Asia', 'Europe', 'Oceania']
    ...      ],
    ...      'layout': Layout(xaxis=XAxis(title='Life Expectancy'), yaxis=YAxis(title='GDP per Capita', type='log'))
    ... }
    >>> html = offline_plotly_data(data, filename=None)

def offline_plotly_data(data, filename=None, config=None, validate=True,
                        default_width='100%', default_height=525, global_requirejs=False):
    r""" Write a plotly scatter plot to HTML file that doesn't require server

    >>> from nlpia.loaders import get_data
    >>> df = get_data('etpinard')  # pd.read_csv('https://plot.ly/~etpinard/191.csv')
    >>> df.columns = [eval(c) if c[0] in '"\'' else str(c) for c in df.columns]
    >>> data = {'data': [
    ...          Scatter(x=df[continent+', x'],
    ...                  y=df[continent+', y'],
    ...                  text=df[continent+', text'],
    ...                  marker=Marker(size=df[continent+', size'].fillna(10000), sizemode='area', sizeref=131868,),
    ...                  mode='markers',
    ...                  name=continent) for continent in ['Africa', 'Americas', 'Asia', 'Europe', 'Oceania']
    ...      ],
    ...      'layout': Layout(xaxis=XAxis(title='Life Expectancy'), yaxis=YAxis(title='GDP per Capita', type='log'))
    ... }
    >>> html = offline_plotly_data(data, filename=None)
    """
    config_default = dict(DEFAULT_PLOTLY_CONFIG)
    if config is not None:
        config_default.update(config)
    with open(os.path.join(DATA_PATH, 'plotly.js.min'), 'rt') as f:
        js = f.read()
    html, divid, width, height = _plot_html(
        data,
        config=config_default,
        validate=validate,
        default_width=default_width, default_height=default_height,
        global_requirejs=global_requirejs)
    html = PLOTLY_HTML.format(plotlyjs=js, plotlyhtml=html)
    if filename and isinstance(filename, str):
        with open(filename, 'wt') as f:
            f.write(html)
    return html