Example #1
0
 def _upload_new_grid(self):
     L.l.info("Uploading new {} grid metadata to plot.ly".format(self.grid_unique_name))
     # grid was not retrieved yet from plotly, create it
     upload_columns = []
     # create column list for grid upload, put first column = x axis
     upload_columns.append(Column(self.columns_cache[self.axis_x_name], self.axis_x_name))
     # then add the remaining columns, except the above one that is already added
     for column_name in self.columns_cache.keys():
         if column_name != self.axis_x_name:
             upload_columns.append(Column(self.columns_cache[column_name], column_name))
     grid = Grid(upload_columns)
     self.grid_url = py.grid_ops.upload(grid,
                  self.grid_unique_name,      # name of the grid in your plotly account
                  world_readable=True, # public or private
                  auto_open=False)      # open the grid in the browser
     # save new uploaded column names to maintain upload order
     for grid_column in upload_columns:
         if grid_column.name not in self.column_name_list_uploaded:
             self.column_name_list_uploaded.append(grid_column.name)
     # save to db cache. expect record to be empty
     plotly_cache_record = models.PlotlyCache().query_filter_first(models.PlotlyCache.grid_name.in_(
         [self.grid_unique_name]))
     if plotly_cache_record:
         L.l.critical("While uploading a new grid found a cached one in DB, unexpected failure!")
     else:
         plotly_cache_record = models.PlotlyCache(grid_name=self.grid_unique_name)
         plotly_cache_record.grid_url = self.grid_url
         my_column_list = ','.join(map(str, self.column_name_list_uploaded))
         plotly_cache_record.column_name_list = my_column_list
         plotly_cache_record.created_by_node_name = Constant.HOST_NAME
         plotly_cache_record.save_changed_fields(new_record=plotly_cache_record, notify_transport_enabled=True,
                                save_to_graph=False)
     L.l.info("Uploading {} grid completed".format(self.grid_unique_name))
Example #2
0
def test_upload_meta_with_grid():
    c1 = Column([1, 2, 3, 4], 'first column')
    Grid([c1])

    unique_filename = _random_filename()

    py.grid_ops.upload(_grid, unique_filename, meta=_meta, auto_open=False)
Example #3
0
def test3():
    column_1 = Column([1, 2, 3], 'column 1')
    column_2 = Column(['a', 'b', utils.get_base_location_now_date()],
                      'column 2')
    grid = Grid([column_1, column_2])

    unique_url = py.grid_ops.upload(grid, 'grid1', world_readable=True)
    print unique_url
Example #4
0
    def test_duplicate_columns(self):
        df = pd.DataFrame([[1, 'a'], [2, 'b']], columns=['col_1', 'col_1'])

        expected_message = ("Yikes, plotly grids currently "
                            "can't have duplicate column names. Rename "
                            "the column \"{}\" and try again.".format('col_1'))

        with self.assertRaisesRegexp(InputError, expected_message):
            Grid(df)
Example #5
0
def test_duplicate_filenames():
    c1 = Column([1, 2, 3, 4], 'first column')
    g = Grid([c1])

    random_chars = [random.choice(string.ascii_uppercase) for _ in range(5)]
    unique_filename = 'Valid Grid ' + ''.join(random_chars)
    py.grid_ops.upload(g, unique_filename, auto_open=False)
    try:
        py.grid_ops.upload(g, unique_filename, auto_open=False)
    except PlotlyRequestError as e:
        assert (e.status_code == 409)
Example #6
0
def test5():
    column_1 = Column([1, 2, 3], 'column 1')
    column_2 = Column(
        ['a', 'b', datetime.datetime.now()],
        'column 2')  # Tabular data can be numbers, strings, or dates
    grid = Grid([column_1, column_2])
    url = py.grid_ops.upload(
        grid,
        'grid example',  # name of the grid in your plotly account
        world_readable=True,  # public or private
        auto_open=True)  # open the grid in the browser
Example #7
0
 def make_the_grid(self,):
     df = self.get_csv_data()
     close = list(df['close'])
     columns = []
     self.n = len(close)
     for i in range(1, self.n):
         x = Column(list(df.index.values)[:i], 'x{}'.format(i - 1))
         columns.append(x)
         y = Column(close[:i], 'y{}'.format(i - 1))
         columns.append(y)
     grid = Grid(columns)
     return grid
Example #8
0
    def test_duplicate_filenames(self):
        c1 = Column([1, 2, 3, 4], 'first column')
        g = Grid([c1])

        random_chars = [random.choice(string.ascii_uppercase)
                        for _ in range(5)]
        unique_filename = 'Valid Grid ' + ''.join(random_chars)
        py.grid_ops.upload(g, unique_filename, auto_open=False)
        try:
            py.grid_ops.upload(g, unique_filename, auto_open=False)
        except PlotlyRequestError as e:
            pass
        else:
            self.fail('Expected this to fail!')
Example #9
0
class MetaTest(PlotlyTestCase):

    _grid = grid = Grid([Column([1, 2, 3, 4], 'first column')])
    _meta = {"settings": {"scope1": {"model": "Unicorn Finder", "voltage": 4}}}

    def setUp(self):
        super(MetaTest, self).setUp()
        py.sign_in('PythonTest', 'xnyU0DEwvAQQCwHVseIL')

    def random_filename(self):
        random_chars = [
            random.choice(string.ascii_uppercase) for _ in range(5)
        ]
        unique_filename = 'Valid Grid with Meta ' + ''.join(random_chars)
        return unique_filename

    @attr('slow')
    def test_upload_meta(self):
        unique_filename = self.random_filename()
        grid_url = py.grid_ops.upload(self._grid,
                                      unique_filename,
                                      auto_open=False)

        # Add some Metadata to that grid
        py.meta_ops.upload(self._meta, grid_url=grid_url)

    @attr('slow')
    def test_upload_meta_with_grid(self):
        c1 = Column([1, 2, 3, 4], 'first column')
        Grid([c1])

        unique_filename = self.random_filename()

        py.grid_ops.upload(self._grid,
                           unique_filename,
                           meta=self._meta,
                           auto_open=False)

    @skip('adding this for now so test_file_tools pass, more info' +
          'https://github.com/plotly/python-api/issues/263')
    def test_metadata_to_nonexistent_grid(self):
        non_exist_meta_url = 'https://local.plot.ly/~GridTest/999999999'
        with self.assertRaises(PlotlyRequestError):
            py.meta_ops.upload(self._meta, grid_url=non_exist_meta_url)
Example #10
0
    # Filter df for current year only
    current_year = df[df.index == int(year)]

    # Get countries and their years ([0] since .valures returns a list of numpy arrays)
    countries = list(current_year.columns.astype(str))
    zvalues = list(current_year.values.astype(float)[0])
    #print(countries, zvalues)

    columns.append(Column(countries, "location{}".format(i + 1)))
    columns.append(Column(zvalues, "z{}".format(i + 1)))

    #print(countries,zvalues)

# Will throw error if file exists or path is not root
grid = Grid(columns)
py.grid_ops.upload(grid, grid_filename, auto_open=False)

# In[]:
# Create data

yellowblue = [[0, "rgb(255,255,204)"], [0.35, "rgb(161,218,180)"],
              [0.5, "rgb(65,182,196)"], [0.6, "rgb(44,127,184)"],
              [0.7, "rgb(8,104,172)"], [1, "rgb(37,52,148)"]]

# Main trace
trace1 = Choropleth(

    # GENERAL
    locationssrc=grid.get_column_reference("location1"),
    zsrc=grid.get_column_reference("z1"),
Example #11
0
columns = []
# make grid
for year in years:
    for continent in continents:
        dataset_by_year = dataset[dataset['year'] == int(year)]
        dataset_by_year_and_cont = dataset_by_year[dataset_by_year['continent'] == continent]
        for col_name in dataset_by_year_and_cont:
            # each column name is unique
            column_name = '{year}_{continent}_{header}_gapminder_grid'.format(
                year=year, continent=continent, header=col_name
            )
            a_column = Column(list(dataset_by_year_and_cont[col_name]), column_name)
            columns.append(a_column)

# upload grid
grid = Grid(columns)
url = py.grid_ops.upload(grid, 'gapminder_grid'+str(time.time()), auto_open=False)

figure = {
    'data': [],
    'layout': {},
    'frames': [],
    'config': {'scrollzoom': True}
}

# fill in most of layout
figure['layout']['xaxis'] = {'range': [30, 85], 'title': 'Life Expectancy', 'gridcolor': '#FFFFFF'}
figure['layout']['yaxis'] = {'title': 'GDP per Capita', 'type': 'log', 'gridcolor': '#FFFFFF'}
figure['layout']['hovermode'] = 'closest'
figure['layout']['plot_bgcolor'] = 'rgb(223, 232, 243)'
Example #12
0
def test_duplicate_columns():
    c1 = Column([1, 2, 3, 4], 'first column')
    c2 = Column(['a', 'b', 'c', 'd'], 'first column')
    Grid([c1, c2])
Example #13
0
def test_row_append_of_non_uploaded_grid():
    c1 = Column([1, 2, 3, 4], 'first column')
    rows = [[1], [2]]
    g = Grid([c1])
    py.grid_ops.append_rows(rows, grid=g)
Example #14
0
def test_column_append_of_non_uploaded_grid():
    c1 = Column([1, 2, 3, 4], 'first column')
    c2 = Column(['a', 'b', 'c', 'd'], 'second column')
    g = Grid([c1])
    py.grid_ops.append_columns([c2], grid=g)
Example #15
0
    columns.append(Column(lons, "x{}".format(i + 1)))
    columns.append(Column(lats, "y{}".format(i + 1)))
    columns.append(Column(texts, "text{}".format(i + 1)))
    columns.append(Column(sizes, "size{}".format(i + 1)))

    columns2.append(Column(xvalues, "x{}".format(i + 1)))
    columns2.append(Column(yvalues, "y{}".format(i + 1)))
    columns2.append(Column(yvalues_cum, "y_cum{}".format(i + 1)))
    columns2.append(Column(blade_avgs, "blade_avg{}".format(i + 1)))
    columns2.append(Column(statevalues, "state{}".format(i + 1)))
    columns2.append(Column(states, "location{}".format(i + 1)))
    columns2.append(Column(zvalues, "z{}".format(i + 1)))

# Will throw error if file exists or path is not root
grid = Grid(columns)
py.grid_ops.upload(grid, grid_filename, auto_open=False)

grid2 = Grid(columns2)
py.grid_ops.upload(grid2, grid_filename2, auto_open=False)

# In[]:
# Create data

viridis = [[0, "rgb(221,42,145)"], [0.3, "rgb(177,77,236)"],
           [0.4, "rgb(118,117,237)"], [0.65, "rgb(46,142,191)"],
           [0.8, "rgb(11,152,121)"], [1, "rgb(19,152,99)"]]

blackpink = [[0, "rgb(59,37,73)"], [0.35, "rgb(76,43,96)"],
             [0.6, "rgb(93,49,119)"], [0.6, "rgb(109,54,143)"],
             [0.7, "rgb(143,66,189)"], [1, "rgb(152,80,200)"]]
Example #16
0
 def test_duplicate_columns(self):
     c1 = Column([1, 2, 3, 4], 'first column')
     c2 = Column(['a', 'b', 'c', 'd'], 'first column')
     with self.assertRaises(InputError):
         Grid([c1, c2])
z = tsne_kmeans_df.z
# hoverTxt_clust_ar = np.array(range(0, len(x), 1))  # Set to row numbers
# hoverTxt_ar = resumes_df.FileNames, '+'(kmeans.labels_)  # Set to file names

resumesPlus_df = resumes_df.copy()
resumesPlus_df['labels'] = kmeans_clusters
resumesPlus_df['labels'] = resumesPlus_df['labels'].apply(str)
resumesPlus_df['labels_file'] = resumesPlus_df['labels'] + ' | ' +\
                                resumesPlus_df.FileNames

hoverTxt_ar = resumesPlus_df['labels_file']

# we want
'5, Report....'

grid = Grid([x, y, z])

trace1 =\
    go.Scatter3d(x=x, y=y, z=z,
                 mode='markers',
                 marker=dict(size=3,
                             color=colormap[kmeans_clusters],
                             line=dict(color='rgb(0,0,0)', width=0.2),
                             opacity=1,),
                 text=hoverTxt_ar)

data = [trace1]
layout = go.Layout(margin=dict(l=0, r=0, b=0, t=0),
                   updatemenus=[
                       dict(
                           type='buttons',
Example #18
0
class GridTest(PlotlyTestCase):

    # Test grid args
    _grid_id = 'chris:3043'
    _grid = Grid([])
    _grid.id = _grid_id
    _grid_url = 'https://plot.ly/~chris/3043/my-grid'

    def setUp(self):
        super(GridTest, self).setUp()
        py.sign_in('PythonTest', 'xnyU0DEwvAQQCwHVseIL')

    def get_grid(self):
        c1 = Column([1, 2, 3, 4], 'first column')
        c2 = Column(['a', 'b', 'c', 'd'], 'second column')
        g = Grid([c1, c2])
        return g

    def upload_and_return_grid(self):
        g = self.get_grid()
        unique_filename = random_filename()
        py.grid_ops.upload(g, unique_filename, auto_open=False)
        return g

    # Nominal usage
    @attr('slow')
    def test_grid_upload(self):
        self.upload_and_return_grid()

    @attr('slow')
    def test_grid_upload_in_new_folder(self):
        g = self.get_grid()
        path = (
            'new folder: {0}/grid in folder {1}'
            .format(random_filename(), random_filename())
        )
        py.grid_ops.upload(g, path, auto_open=False)

    @attr('slow')
    def test_grid_upload_in_existing_folder(self):
        g = self.get_grid()
        folder = random_filename()
        filename = random_filename()
        py.file_ops.mkdirs(folder)
        path = (
            'existing folder: {0}/grid in folder {1}'
            .format(folder, filename)
        )
        py.grid_ops.upload(g, path, auto_open=False)

    @attr('slow')
    def test_column_append(self):
        g = self.upload_and_return_grid()
        new_col = Column([1, 5, 3], 'new col')
        py.grid_ops.append_columns([new_col], grid=g)

    @attr('slow')
    def test_row_append(self):
        g = self.upload_and_return_grid()
        new_rows = [[1, 2], [10, 20]]
        py.grid_ops.append_rows(new_rows, grid=g)

    @attr('slow')
    def test_plot_from_grid(self):
        g = self.upload_and_return_grid()
        url = py.plot([Scatter(xsrc=g[0].id, ysrc=g[1].id)],
                      auto_open=False, filename='plot from grid')
        return url, g

    @attr('slow')
    def test_get_figure_from_references(self):
        url, g = self.test_plot_from_grid()
        fig = py.get_figure(url)
        data = fig['data']
        trace = data[0]
        assert(tuple(g[0].data) == tuple(trace['x']))
        assert(tuple(g[1].data) == tuple(trace['y']))

    def test_grid_id_args(self):
        self.assertEqual(parse_grid_id_args(self._grid, None),
                         parse_grid_id_args(None, self._grid_url))

    def test_no_grid_id_args(self):
        with self.assertRaises(InputError):
            parse_grid_id_args(None, None)

    def test_overspecified_grid_args(self):
        with self.assertRaises(InputError):
            parse_grid_id_args(self._grid, self._grid_url)

    # not broken anymore since plotly 3.0.0
    # def test_scatter_from_non_uploaded_grid(self):
    #     c1 = Column([1, 2, 3, 4], 'first column')
    #     c2 = Column(['a', 'b', 'c', 'd'], 'second column')
    #     g = Grid([c1, c2])
    #     with self.assertRaises(ValueError):
    #         Scatter(xsrc=g[0], ysrc=g[1])

    def test_column_append_of_non_uploaded_grid(self):
        c1 = Column([1, 2, 3, 4], 'first column')
        c2 = Column(['a', 'b', 'c', 'd'], 'second column')
        g = Grid([c1])
        with self.assertRaises(PlotlyError):
            py.grid_ops.append_columns([c2], grid=g)

    def test_row_append_of_non_uploaded_grid(self):
        c1 = Column([1, 2, 3, 4], 'first column')
        rows = [[1], [2]]
        g = Grid([c1])
        with self.assertRaises(PlotlyError):
            py.grid_ops.append_rows(rows, grid=g)

    # Input Errors
    @attr('slow')
    def test_unequal_length_rows(self):
        g = self.upload_and_return_grid()
        rows = [[1, 2], ['to', 'many', 'cells']]
        with self.assertRaises(InputError):
            py.grid_ops.append_rows(rows, grid=g)

    # Test duplicate columns
    def test_duplicate_columns(self):
        c1 = Column([1, 2, 3, 4], 'first column')
        c2 = Column(['a', 'b', 'c', 'd'], 'first column')
        with self.assertRaises(InputError):
            Grid([c1, c2])

    # Test delete
    @attr('slow')
    def test_delete_grid(self):
        g = self.get_grid()
        fn = random_filename()
        py.grid_ops.upload(g, fn, auto_open=False)
        py.grid_ops.delete(g)
        py.grid_ops.upload(g, fn, auto_open=False)

    # Plotly failures
    @skip('adding this for now so test_file_tools pass, more info' +
          'https://github.com/plotly/python-api/issues/262')
    def test_duplicate_filenames(self):
        c1 = Column([1, 2, 3, 4], 'first column')
        g = Grid([c1])

        random_chars = [random.choice(string.ascii_uppercase)
                        for _ in range(5)]
        unique_filename = 'Valid Grid ' + ''.join(random_chars)
        py.grid_ops.upload(g, unique_filename, auto_open=False)
        try:
            py.grid_ops.upload(g, unique_filename, auto_open=False)
        except PlotlyRequestError as e:
            pass
        else:
            self.fail('Expected this to fail!')
Example #19
0
 def test_scatter_from_non_uploaded_grid(self):
     c1 = Column([1, 2, 3, 4], 'first column')
     c2 = Column(['a', 'b', 'c', 'd'], 'second column')
     g = Grid([c1, c2])
     with self.assertRaises(InputError):
         Scatter(xsrc=g[0], ysrc=g[1])
    [0.7, 'rgb(156, 184, 188)'],
    [0.75, 'rgb(168, 199, 199)'],
    [0.8, 'rgb(185, 210, 210)'],
    [0.85, 'rgb(203, 221, 221)'],
    [0.9, 'rgb(220, 233, 233)'],
    [0.95, 'rgb(238, 244, 244)'],
    [1.0, 'rgb(255, 255, 255)']]

my_columns = []
nr_frames = 68
for k in range(nr_frames):
    my_columns.extend(
        [Column((6.7 - k * 0.1) * np.ones((r, c)), 'z{}'.format(k + 1)),
         Column(np.flipud(volume[67 - k]), 'surfc{}'.format(k + 1))]
    )
grid = Grid(my_columns)
py.grid_ops.upload(grid, 'anim_sliceshead'+str(time.time()), auto_open=False)

data=[
    dict(
        type='surface', 
        zsrc=grid.get_column_reference('z1'),
        surfacecolorsrc=grid.get_column_reference('surfc1'),
        colorscale=pl_bone,
        colorbar=dict(thickness=20, ticklen=4)
    )
]

frames=[]
for k in range(nr_frames):
    frames.append(
             "{}".format(lines[27]), "{}".format(lines[32]), ""
         ],
         [
             "{}".format(lines[1]), "{}".format(lines[6]),
             "{}".format(lines[11]), "", "{}".format(lines[19]), "",
             "{}".format(lines[26]), "{}".format(lines[31]), ""
         ],
         [
             "{}".format(lines[0]), "{}".format(lines[5]),
             "{}".format(lines[10]), "{}".format(lines[15]),
             "{}".format(lines[18]), "{}".format(lines[22]),
             "{}".format(lines[25]), "{}".format(lines[30]), ""
         ]]
    my_columns.append(Column(z, 'z{}'.format(i)))
conn.close()
grid = Grid(my_columns)
py.grid_ops.upload(grid, 'values' + str(time.time()), auto_open=False)

data = [
    dict(
        type='heatmap',  #Create one heatmap here 
        xsrc=grid.get_column_reference('x'),
        ysrc=grid.get_column_reference('y'),
        zsrc=grid.get_column_reference('z1'),
        zmin=-90,
        zmax=-20,
        zsmooth='best',
        #colorscale=colorscale,
        colorbar=dict(thickness=20, ticklen=4))
]
Example #22
0
grid_filename = chart_filename + " Grid"
columns = []

for i, year in enumerate(years):

    lons = df[df["YEAR"] == int(year)]["LON"].astype(float)
    lats = df[df["YEAR"] == int(year)]["LAT"].astype(float)
    texts = df[df["YEAR"] == int(year)]["STRCITY"].astype(str)

    columns.append(Column(lons, "x{}".format(i + 1)))
    columns.append(Column(lats, "y{}".format(i + 1)))
    columns.append(Column(texts, "text{}".format(i + 1)))

# Will throw error if file exists or path is not root
grid = Grid(columns)
py.grid_ops.upload(grid, grid_filename, auto_open=False)

# In[]:
# Create data

trace1 = Scattermapbox(

    # GENERAL
    lonsrc=grid.get_column_reference("x1"),
    latsrc=grid.get_column_reference("y1"),
    textsrc=grid.get_column_reference("text1"),
    hoverinfo="lon+lat+text",
    mode="markers",
    marker=dict(
        size=10,
Example #23
0
 def test_column_append_of_non_uploaded_grid(self):
     c1 = Column([1, 2, 3, 4], 'first column')
     c2 = Column(['a', 'b', 'c', 'd'], 'second column')
     g = Grid([c1])
     with self.assertRaises(PlotlyError):
         py.grid_ops.append_columns([c2], grid=g)
Example #24
0
 def test_row_append_of_non_uploaded_grid(self):
     c1 = Column([1, 2, 3, 4], 'first column')
     rows = [[1], [2]]
     g = Grid([c1])
     with self.assertRaises(PlotlyError):
         py.grid_ops.append_rows(rows, grid=g)
Example #25
0
    return url, g


@with_setup(init)
def test_get_figure_from_references():
    url, g = test_plot_from_grid()
    fig = py.get_figure(url)
    data = fig['data']
    trace = data[0]
    assert (g[0].data == trace['x'])
    assert (g[1].data == trace['y'])


## Test grid args
_grid_id = 'chris:3043'
_grid = Grid([])
_grid.id = _grid_id
_grid_url = 'https://plot.ly/~chris/3043/my-grid'


def test_grid_id_args():
    assert (_api_v2.parse_grid_id_args(_grid,
                                       None) == _api_v2.parse_grid_id_args(
                                           None, _grid_url))


@raises(InputError)
def test_no_grid_id_args():
    _api_v2.parse_grid_id_args(None, None)

Example #26
0
def test_scatter_from_non_uploaded_grid():
    c1 = Column([1, 2, 3, 4], 'first column')
    c2 = Column(['a', 'b', 'c', 'd'], 'second column')
    g = Grid([c1, c2])

    Scatter(xsrc=g[0], ysrc=g[1])
Example #27
0
from nose import with_setup
from nose.plugins.attrib import attr
from nose.tools import raises
from unittest import skip

import plotly.plotly as py
from plotly.exceptions import PlotlyRequestError
from plotly.grid_objs import Column, Grid


def init():
    py.sign_in('PythonTest', '9v9f20pext')


_grid = grid = Grid([Column([1, 2, 3, 4], 'first column')])
_meta = {"settings": {"scope1": {"model": "Unicorn Finder", "voltage": 4}}}


def _random_filename():
    random_chars = [random.choice(string.ascii_uppercase) for _ in range(5)]
    unique_filename = 'Valid Grid with Meta ' + ''.join(random_chars)
    return unique_filename


@attr('slow')
@with_setup(init)
def test_upload_meta():
    unique_filename = _random_filename()
    grid_url = py.grid_ops.upload(_grid, unique_filename, auto_open=False)
Example #28
0
 def get_grid(self):
     c1 = Column([1, 2, 3, 4], 'first column')
     c2 = Column(['a', 'b', 'c', 'd'], 'second column')
     g = Grid([c1, c2])
     return g
Example #29
0
chart_filename = "Pendulum " + str(datetime.now())
grid_filename = chart_filename + " Grid"
columns = []

# Actual animation function
for i in range(1000):

    pendulum.step(dt)
    x, y = pendulum.position()
    x = list(x)
    y = list(y)

    columns.append(Column(x, "x{}".format(i + 1)))
    columns.append(Column(y, "y{}".format(i + 1)))

grid = Grid(columns)
py.grid_ops.upload(grid, grid_filename, auto_open=False)


# In[]:
# Create data

trace1 = Scatter(
    xsrc = grid.get_column_reference("x1"),
    ysrc = grid.get_column_reference("y1"),
    mode = "lines+markers",
)


# In[]:
# Create layout
Example #30
0
                  auto_open=False, filename='plot from grid')
    return url, g


@with_setup(init)
def test_get_figure_from_references():
    url, g = test_plot_from_grid()
    fig = py.get_figure(url)
    data = fig['data']
    trace = data[0]
    assert(g[0].data == trace['x'])
    assert(g[1].data == trace['y'])

## Test grid args
_grid_id = 'chris:3043'
_grid = Grid([])
_grid.id = _grid_id
_grid_url = 'https://plot.ly/~chris/3043/my-grid'


def test_grid_id_args():
    assert(
        _api_v2.parse_grid_id_args(_grid, None) ==
        _api_v2.parse_grid_id_args(None, _grid_url)
    )


@raises(InputError)
def test_no_grid_id_args():
    _api_v2.parse_grid_id_args(None, None)
Example #31
0
def animated_chart(merged_df):
    '''
    Gera um plot animado. O eixo x merged_df.columns[3], sendo o eixo y merged_df.columns[2], a legenda sendo os continentes, o tamanho dos circulos sendo merged_df.columns[4] e o controle deslizante é a variação dos anos de 1975 a 2014.
    
    Parameters:
    -----------
    
        merged_df -> dataframe de 5 colunas, a configuração do gráfico exige formatação correta da coluna:
        eixo x merged_df.columns[3], 
        sendo o eixo y merged_df.columns[2], 
        A legenda sendo os continentes, 
        O tamanho dos circulos sendo merged_df.columns[4] 
        O controle deslizante é a variação dos anos de 1975 a 2014.
        
    Return:
    -------
        
        Um gráfico animado.
    
    Example:
    --------
    
        animated_chart(merged_df)     
    '''
    #Normalizando os dados para impedir problemas de escala:
    norm = lambda df: preprocessing.MinMaxScaler().fit_transform(
        np.array(df, dtype="float64").reshape(-1, 1))
    for i in merged_df.columns[2:-1]:
        merged_df[i] = norm(merged_df[i])

    #Criando lista de anos e continentes para usar na legenda e controle deslizante:
    List_of_years = list(range(1975, 2015))

    List_Of_Continents = list(set(Continents.values()))

    merged_df['Year'] = merged_df['Year'].apply(int)

    columns = []
    # Fazendo o grid:
    for year in List_of_years:
        for continent in List_Of_Continents:
            dataset_by_year = merged_df[merged_df['Year'] == int(year)]
            dataset_by_year_and_cont = dataset_by_year[
                dataset_by_year['Continents'] == continent]
            for col_name in dataset_by_year_and_cont:
                column_name = '{year}_{continent}_{header}_gapminder_grid'.format(
                    year=year, continent=continent, header=col_name)
                a_column = Column(list(dataset_by_year_and_cont[col_name]),
                                  column_name)
                columns.append(a_column)

    grid = Grid(columns)
    url = py.grid_ops.upload(grid,
                             'GDP.rateplot' + str(time.time()),
                             auto_open=False)
    url

    #Criando a figura:

    figure1 = {
        'data': [],
        'layout': {},
        'frames': [],
        'config': {
            'scrollzoom': True
        }
    }

    figure1['layout']['xaxis'] = {
        'title': list(merged_df.columns)[3],
        'gridcolor': '#FFFFFF'
    }
    figure1['layout']['yaxis'] = {
        'title': list(merged_df.columns)[2],
        'type': 'log',
        'gridcolor': '#FFFFFF'
    }
    figure1['layout']['hovermode'] = 'closest'
    figure1['layout']['plot_bgcolor'] = 'rgb(223, 232, 243)'

    #Criando Controle Deslizante:

    sliders_dict = {
        'active': 0,
        'yanchor': 'top',
        'xanchor': 'left',
        'currentvalue': {
            'font': {
                'size': 20
            },
            'prefix': 'Year:',
            'visible': True,
            'xanchor': 'right'
        },
        'transition': {
            'duration': 300,
            'easing': 'cubic-in-out'
        },
        'pad': {
            'b': 10,
            't': 50
        },
        'len': 0.9,
        'x': 0.1,
        'y': 0,
        'steps': []
    }

    #Criando legendas:

    figure1['layout']['updatemenus'] = [{
        'buttons': [{
            'args': [
                None, {
                    'frame': {
                        'duration': 500,
                        'redraw': False
                    },
                    'fromcurrent': True,
                    'transition': {
                        'duration': 300,
                        'easing': 'quadratic-in-out'
                    }
                }
            ],
            'label':
            'Play',
            'method':
            'animate'
        }, {
            'args': [[None], {
                'frame': {
                    'duration': 0,
                    'redraw': False
                },
                'mode': 'immediate',
                'transition': {
                    'duration': 0
                }
            }],
            'label':
            'Pause',
            'method':
            'animate'
        }],
        'direction':
        'left',
        'pad': {
            'r': 10,
            't': 87
        },
        'showactive':
        False,
        'type':
        'buttons',
        'x':
        0.1,
        'xanchor':
        'right',
        'y':
        0,
        'yanchor':
        'top'
    }]

    custom_colors = {
        'East Asia & Pacific': 'rgb(171, 99, 250)',
        'Europe & Central Asia': 'rgb(230, 99, 250)',
        'Africa': 'rgb(99, 110, 250)',
        'Latin America & Caribbean': 'rgb(25, 211, 243)',
        'Middle East & North Africa': 'rgb(50, 170, 255)',
        'North America': 'rgb(215,20,20)',
        'South Asia': 'rgb(20,215,20)',
        'Sub-Saharan Africa': 'rgb(0,0,0)',
    }
    #Criando layout da figura:
    col_name_template = '{year}_{continent}_{header}_gapminder_grid'
    year = 1975
    for continent in List_Of_Continents:
        data_dict = {
            'xsrc':
            grid.get_column_reference(
                col_name_template.format(year=year,
                                         continent=continent,
                                         header=list(merged_df.columns)[3])),
            'ysrc':
            grid.get_column_reference(
                col_name_template.format(year=year,
                                         continent=continent,
                                         header=list(merged_df.columns)[2])),
            'mode':
            'markers',
            'textsrc':
            grid.get_column_reference(
                col_name_template.format(year=year,
                                         continent=continent,
                                         header='Country')),
            'marker': {
                'sizemode':
                'area',
                'sizeref':
                0.005,
                'sizesrc':
                grid.get_column_reference(
                    col_name_template.format(year=year,
                                             continent=continent,
                                             header=list(
                                                 merged_df.columns)[4])),
                'color':
                custom_colors[continent]
            },
            'name':
            continent
        }
        figure1['data'].append(data_dict)
    for year in List_of_years:
        frame = {'data': [], 'name': str(year)}
        for continent in List_Of_Continents:
            data_dict = {
                'xsrc':
                grid.get_column_reference(
                    col_name_template.format(year=year,
                                             continent=continent,
                                             header=list(
                                                 merged_df.columns)[3])),
                'ysrc':
                grid.get_column_reference(
                    col_name_template.format(year=year,
                                             continent=continent,
                                             header=list(
                                                 merged_df.columns)[2])),
                'mode':
                'markers',
                'textsrc':
                grid.get_column_reference(
                    col_name_template.format(year=year,
                                             continent=continent,
                                             header='Country')),
                'marker': {
                    'sizemode':
                    'area',
                    'sizeref':
                    0.005,
                    'sizesrc':
                    grid.get_column_reference(
                        col_name_template.format(year=year,
                                                 continent=continent,
                                                 header=list(
                                                     merged_df.columns)[4])),
                    'color':
                    custom_colors[continent]
                },
                'name':
                continent
            }
            frame['data'].append(data_dict)

        figure1['frames'].append(frame)
        slider_step = {
            'args': [[year], {
                'frame': {
                    'duration': 300,
                    'redraw': False
                },
                'mode': 'immediate',
                'transition': {
                    'duration': 300
                }
            }],
            'label':
            year,
            'method':
            'animate'
        }
        sliders_dict['steps'].append(slider_step)

    figure1['layout']['sliders'] = [sliders_dict]

    #Plotando:
    return py.icreate_animations(figure1, 'Plot1CreditGDP' + str(time.time()))