Ejemplo n.º 1
0
    def create_map(self, path='map.html', plugin_data_out=True, template=None):
        '''Write Map output to HTML and data output to JSON if available

        Parameters:
        -----------
        path: string, default 'map.html'
            Path for HTML output for map
        plugin_data_out: boolean, default True
            If using plugins such as awesome markers, write all plugin
            data such as JS/CSS/images to path
        template: string, default None
            Custom template to render

        '''
        self.map_path = path
        self._build_map(template)

        with codecs.open(path, 'w', 'utf8') as f:
            f.write(self.HTML)

        if self.json_data:
            for path, data in iteritems(self.json_data):
                with open(path, 'w') as g:
                    json.dump(data, g)

        if self.plugins and plugin_data_out:
            for name, plugin in iteritems(self.plugins):
                with open(name, 'w') as f:
                    if isinstance(plugin, binary_type):
                        plugin = text_type(plugin, 'utf8')
                    f.write(plugin)
Ejemplo n.º 2
0
    def create_map(self, path='map.html', plugin_data_out=True, template=None):
        """Write Map output to HTML and data output to JSON if available.

        Parameters:
        -----------
        path: string, default 'map.html'
            Path for HTML output for map
        plugin_data_out: boolean, default True
            If using plugins such as awesome markers, write all plugin
            data such as JS/CSS/images to path
        template: string, default None
            Custom template to render

        """
        self.map_path = path
        self._build_map(template)

        with codecs.open(path, 'w', 'utf8') as f:
            f.write(self.HTML)

        if self.json_data:
            for path, data in iteritems(self.json_data):
                with open(path, 'w') as g:
                    json.dump(data, g)

        if self.plugins and plugin_data_out:
            for name, plugin in iteritems(self.plugins):
                with open(name, 'w') as f:
                    if isinstance(plugin, binary_type):
                        plugin = text_type(plugin, 'utf8')
                    f.write(plugin)
Ejemplo n.º 3
0
def transform_data(data):
    """
    Transform Pandas DataFrame into JSON format.

    Parameters
    ----------
    data: DataFrame or Series
        Pandas DataFrame or Series

    Returns
    -------
    JSON compatible dict

    Example
    -------
    >>> transform_data(df)

    """
    if pd is None:
        raise ImportError("The Pandas package is required"
                          " for this functionality")

    if np is None:
        raise ImportError("The NumPy package is required"
                          " for this functionality")

    def type_check(value):
        """
        Type check values for JSON serialization. Native Python JSON
        serialization will not recognize some Numpy data types properly,
        so they must be explicitly converted.

        """
        if pd.isnull(value):
            return None
        elif (isinstance(value, pd.tslib.Timestamp)
              or isinstance(value, pd.Period)):
            return time.mktime(value.timetuple())
        elif isinstance(value, (int, np.integer)):
            return int(value)
        elif isinstance(value, (float, np.float_)):
            return float(value)
        elif isinstance(value, str):
            return str(value)
        else:
            return value

    if isinstance(data, pd.Series):
        json_data = [{
            type_check(x): type_check(y)
            for x, y in iteritems(data)
        }]
    elif isinstance(data, pd.DataFrame):
        json_data = [{
            type_check(y): type_check(z)
            for x, y, z in data.itertuples()
        }]

    return json_data
Ejemplo n.º 4
0
def transform_data(data):
    """
    Transform Pandas DataFrame into JSON format.

    Parameters
    ----------
    data: DataFrame or Series
        Pandas DataFrame or Series

    Returns
    -------
    JSON compatible dict

    Example
    -------
    >>> transform_data(df)

    """
    if pd is None:
        raise ImportError("The Pandas package is required"
                          " for this functionality")

    if np is None:
        raise ImportError("The NumPy package is required"
                          " for this functionality")

    def type_check(value):
        """
        Type check values for JSON serialization. Native Python JSON
        serialization will not recognize some Numpy data types properly,
        so they must be explicitly converted.

        """
        if pd.isnull(value):
            return None
        elif (isinstance(value, pd.tslib.Timestamp) or
              isinstance(value, pd.Period)):
            return time.mktime(value.timetuple())
        elif isinstance(value, (int, np.integer)):
            return int(value)
        elif isinstance(value, (float, np.float_)):
            return float(value)
        elif isinstance(value, str):
            return str(value)
        else:
            return value

    if isinstance(data, pd.Series):
        json_data = [{type_check(x): type_check(y) for
                      x, y in iteritems(data)}]
    elif isinstance(data, pd.DataFrame):
        json_data = [{type_check(y): type_check(z) for
                      x, y, z in data.itertuples()}]

    return json_data