gramex.data

Query and manipule data from any source.

filter(url, args={}, meta={}, engine=None, table=None, ext=None, id=None, columns=None, query=None, queryfile=None, transform=None, transform_kwargs={}, argstype={}, kwargs)

Filter data using URL query parameters.

Examples:

>>> gramex.data.filter(dataframe, args=handler.args)
>>> gramex.data.filter('file.csv', args=handler.args)
>>> gramex.data.filter('mysql://server/db', table='table', args={'user': [user']})

Parameters:

Name Type Description Default
url Union[str, pd.DataFrame]

DataFrame, sqlalchemy URL, directory or file name, http(s) URL

required
args dict

URL query parameters as a dict of lists. Pass handler.args or parse_qs results

{}
meta dict

this dict is updated with metadata during the course of filtering

{}
engine str

over-rides the auto-detected engine. Can be ‘dataframe’, ‘file’, ‘http’, ‘https’, ‘sqlalchemy’, ‘dir’

None
ext str

file extension (if url is a file). Defaults to url extension

None
columns Dict[str, Union[str, dict]]

database column names to create if required (if url is a database). Keys are column names. Values can be SQL types, or dicts with these keys: - type (str), e.g. "VARCHAR(10)" - default (str/int/float/bool), e.g. "none@example.org" - nullable (bool), e.g. False - primary_key (bool), e.g. True – used only when creating new tables - autoincrement (bool), e.g. True – used only when creating new tables

None
query str

optional SQL query to execute (if url is a database), .format-ed using args and supports SQLAlchemy SQL parameters. Loads entire result in memory before filtering.

None
queryfile str

optional SQL query file to execute (if url is a database). Same as specifying the query: in a file. Overrides query:

None
transform Callable

optional in-memory transform of source data. Takes the result of gramex.cache.open or gramex.cache.query. Must return a DataFrame. Applied to both file and SQLAlchemy urls.

None
transform_kwargs dict

optional keyword arguments to be passed to the transform function – apart from data

{}
argstype Dict[str, dict]

optional dict that specifies args type and behavior for query.

{}
**kwargs dict

Additional parameters are passed to gramex.cache.open, sqlalchemy.create_engine or the plugin’s filter

{}

Returns:

Type Description
pd.DataFrame

Filtered DataFrame

To filter a DataFrame where column x=1 and y=2:

>>> filtered = gramex.data.filter(dataframe, args={'x': [1], 'y': [2]})

args is always a dict of lists, which is compatible with Gramex’s handler.args.

So you can replace the above with

filtered = gramex.data.filter(dataframe, args=handle.args)

To filter ?city=Rome from a CSV/XLS/any file supported by gramex.cache.open:

>>> gramex.data.filter('path/to/file.csv', rel=True, args={'city': ['Rome']})

Remaining kwargs are passed to gramex.cache.open if url is a file. So rel=True works

To filter ?city=Rome from a SQLite, MySQL, PostgreSQL or any SQLAlchemy-supported DB:

>>> gramex.data.filter('sqlite:///x.db', table='data', args={'city': ['Rome']})

Remaining kwargs are passed to sqlalchemy.create_engine if url is a SQLAlchemy URL. E.g.

  • table: table name (if url is an SQLAlchemy URL), .format-ed using args.
  • state: optional SQL query to check if data has changed.

TODO: Document how to pass params – for each database

If table or query is passed to an SQLAlchemy url, it is formatted using args. For example

>>> data = gramex.data.filter('mysql://server/db', table='{xxx}', args=handler.args)

… when passed ?xxx=sales returns rows from the sales table. Similarly:

>>> data = gramex.data.filter(
...     'mysql://server/db', args=handler.args,
...     query='SELECT {col}, COUNT(*) FROM table GROUP BY {col}')

… when passsed ?col=City replaces {col} with City.

NOTE: To avoid SQL injection attacks, only keys without spaces are allowed. So ?city name=Oslo will not work.

The URL supports operators filter like this:

  • ?x selects x is not null
  • ?x! selects x is null
  • ?x=val selects x == val
  • ?x!=val selects x != val
  • ?x>=val selects x > val
  • ?x>~=val selects x >= val
  • ?x<=val selects x < val
  • ?x<~=val selects x <= val
  • ?x~=val selects x matches val
  • ?x!~=val selects x does not match val
  • ?x*=val selects x matches val case-insensitively
  • ?x!*=val selects x does not match val case-insensitively

Multiple filters are combined into an AND clause. Ranges can also be specified like this:

  • ?x=a&y=b selects x = a AND y = b
  • ?x>=100&x<=200 selects x > 100 AND x < 200

If the same column has multiple values, they are combined like this:

  • ?x=a&x=b selects x IN (a, b)
  • ?x!=a&x!=b selects x NOT IN (a, b)
  • ?x~=a&x~=b selects x ~ a|b
  • ?x>=a&x>=b selects x > MIN(a, b)
  • ?x<=a&x<=b selects x < MAX(a, b)

Arguments are converted to the type of the column before comparing. If this fails, it raises a ValueError.

You can specify the SQL argument type for database queries with argstype. This (currently) only applies when using query or queryfunction, not table:

  • argstype: {'x': {type: float}, 'y': {type: bool}} treats x as a float and y as a bool
  • argstype: {'x': {type: int, expanding=True}} treats x as a list of int, suitable for use in an IN clause, e.g. SELECT * FROM table WHERE size IN :x

These URL query parameters control the output:

  • ?_sort=col sorts column col in ascending order. ?_sort=-col sorts in descending order.
  • ?_limit=100 limits the result to 100 rows
  • ?_offset=100 starts showing the result from row 100. Default: 0
  • ?_c=x&_c=y returns only columns [x, y]. ?_c=-col drops col.

If a column name matches one of the above, you cannot filter by that column. Avoid column names beginning with _.

To get additional information about the filtering, use:

meta = {}  # Create a variable which will be filled with more info
filtered = gramex.data.filter(data, meta=meta, **handler.args)

The meta variable is populated with the following keys:

  • filters: Applied filters as [(col, op, val), ...]
  • ignored: Ignored filters as [(col, vals), ('_sort', col), ('_by', col), ...]
  • excluded: Excluded columns as [col, ...]
  • sort: Sorted columns as [(col, True), ...]. The second parameter is ascending=
  • offset: Offset as integer. Defaults to 0
  • limit: Limit as integer - None if limit is not applied
  • count: Total number of rows, if available
  • by: Group by columns as [col, ...]
  • inserted: List of (dict of primary values) for each inserted row

These variables may be useful to show additional information about the filtered data.

Source code in gramex\data.py
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
def filter(
    url: Union[str, pd.DataFrame],
    args: dict = {},
    meta: dict = {},
    engine: str = None,
    table: str = None,
    ext: str = None,
    id: List[str] = None,
    columns: Dict[str, Union[str, dict]] = None,
    query: str = None,
    queryfile: str = None,
    transform: Callable = None,
    transform_kwargs: dict = {},
    argstype: Dict[str, dict] = {},
    **kwargs: dict,
) -> pd.DataFrame:
    '''Filter data using URL query parameters.

    Examples:
        >>> gramex.data.filter(dataframe, args=handler.args)
        >>> gramex.data.filter('file.csv', args=handler.args)
        >>> gramex.data.filter('mysql://server/db', table='table', args={'user': [user']})

    Parameters:

        url: DataFrame, sqlalchemy URL, directory or file name, http(s) URL
        args: URL query parameters as a dict of lists. Pass handler.args or parse_qs results
        meta: this dict is updated with metadata during the course of filtering
        engine: over-rides the auto-detected engine. Can be 'dataframe', 'file',
            'http', 'https', 'sqlalchemy', 'dir'
        ext: file extension (if url is a file). Defaults to url extension
        columns: database column names to create if required (if url is a database).
            Keys are column names. Values can be SQL types, or dicts with these keys:
                - `type` (str), e.g. `"VARCHAR(10)"`
                - `default` (str/int/float/bool), e.g. `"none@example.org"`
                - `nullable` (bool), e.g. `False`
                - `primary_key` (bool), e.g. `True` -- used only when creating new tables
                - `autoincrement` (bool), e.g. `True` -- used only when creating new tables
        query: optional SQL query to execute (if url is a database),
            `.format`-ed using `args` and supports SQLAlchemy SQL parameters.
            Loads entire result in memory before filtering.
        queryfile: optional SQL query file to execute (if url is a database).
            Same as specifying the `query:` in a file. Overrides `query:`
        transform: optional in-memory transform of source data. Takes
            the result of gramex.cache.open or gramex.cache.query. Must return a
            DataFrame. Applied to both file and SQLAlchemy urls.
        transform_kwargs: optional keyword arguments to be passed to the
            transform function -- apart from data
        argstype: optional dict that specifies `args` type and behavior for `query`.
        **kwargs: Additional parameters are passed to
            [gramex.cache.open][], `sqlalchemy.create_engine` or the plugin's filter

    Returns:
        Filtered DataFrame

    To filter a DataFrame where column x=1 and y=2:

        >>> filtered = gramex.data.filter(dataframe, args={'x': [1], 'y': [2]})

    `args` is always a dict of lists, which is compatible with Gramex's `handler.args`.
    So you can replace the above with:

        >>> filtered = gramex.data.filter(dataframe, args=handle.args)

    To filter ?city=Rome from a CSV/XLS/any file supported by [gramex.cache.open][]:

        >>> gramex.data.filter('path/to/file.csv', rel=True, args={'city': ['Rome']})

    Remaining `kwargs` are passed to [gramex.cache.open][] if `url` is a file. So `rel=True` works

    To filter ?city=Rome from a SQLite, MySQL, PostgreSQL or any SQLAlchemy-supported DB:

        >>> gramex.data.filter('sqlite:///x.db', table='data', args={'city': ['Rome']})

    Remaining `kwargs` are passed to `sqlalchemy.create_engine` if `url` is a SQLAlchemy URL. E.g.

    - `table`: table name (if url is an SQLAlchemy URL), `.format`-ed using `args`.
    - `state`: optional SQL query to check if data has changed.

    TODO: Document how to pass params -- for each database

    If `table` or `query` is passed to an SQLAlchemy url, it is formatted using `args`.
    For example

        >>> data = gramex.data.filter('mysql://server/db', table='{xxx}', args=handler.args)

    ... when passed `?xxx=sales` returns rows from the sales table. Similarly:

        >>> data = gramex.data.filter(
        ...     'mysql://server/db', args=handler.args,
        ...     query='SELECT {col}, COUNT(*) FROM table GROUP BY {col}')

    ... when passsed `?col=City` replaces `{col}` with `City`.

    NOTE: To avoid SQL injection attacks, only keys without spaces are allowed.
    So `?city name=Oslo` **will not** work.

    The URL supports operators filter like this:

    - `?x` selects x is not null
    - `?x!` selects x is null
    - `?x=val` selects x == val
    - `?x!=val` selects x != val
    - `?x>=val` selects x > val
    - `?x>~=val` selects x >= val
    - `?x<=val` selects x < val
    - `?x<~=val` selects x <= val
    - `?x~=val` selects x matches val
    - `?x!~=val` selects x does not match val
    - `?x*=val` selects x matches val case-insensitively
    - `?x!*=val` selects x does not match val case-insensitively

    Multiple filters are combined into an AND clause. Ranges can also be
    specified like this:

    - `?x=a&y=b` selects x = a AND y = b
    - `?x>=100&x<=200` selects x > 100 AND x < 200

    If the same column has multiple values, they are combined like this:

    - `?x=a&x=b` selects x IN (a, b)
    - `?x!=a&x!=b` selects x NOT IN (a, b)
    - `?x~=a&x~=b` selects x ~ a|b
    - `?x>=a&x>=b` selects x > MIN(a, b)
    - `?x<=a&x<=b` selects x < MAX(a, b)

    Arguments are converted to the type of the column before comparing. If this
    fails, it raises a ValueError.

    You can specify the SQL argument type for database queries with `argstype`. This (currently)
    only applies when using `query` or `queryfunction`, not `table`:

    - `argstype: {'x': {type: float}, 'y': {type: bool}}` treats x as a float and y as a bool
    - `argstype: {'x': {type: int, expanding=True}}` treats x as a list of int, suitable for
      use in an `IN` clause, e.g. `SELECT * FROM table WHERE size IN :x`

    These URL query parameters control the output:

    - `?_sort=col` sorts column col in ascending order. `?_sort=-col` sorts
        in descending order.
    - `?_limit=100` limits the result to 100 rows
    - `?_offset=100` starts showing the result from row 100. Default: 0
    - `?_c=x&_c=y` returns only columns `[x, y]`. `?_c=-col` drops col.

    If a column name matches one of the above, you cannot filter by that column.
    Avoid column names beginning with _.

    To get additional information about the filtering, use:

        meta = {}  # Create a variable which will be filled with more info
        filtered = gramex.data.filter(data, meta=meta, **handler.args)

    The `meta` variable is populated with the following keys:

    - `filters`: Applied filters as `[(col, op, val), ...]`
    - `ignored`: Ignored filters as `[(col, vals), ('_sort', col), ('_by', col), ...]`
    - `excluded`: Excluded columns as `[col, ...]`
    - `sort`: Sorted columns as `[(col, True), ...]`. The second parameter is `ascending=`
    - `offset`: Offset as integer. Defaults to 0
    - `limit`: Limit as integer - `None` if limit is not applied
    - `count`: Total number of rows, if available
    - `by`: Group by columns as `[col, ...]`
    - `inserted`: List of (dict of primary values) for each inserted row

    These variables may be useful to show additional information about the
    filtered data.
    '''
    # Auto-detect engine.
    if engine is None:
        engine = get_engine(url)

    # Pass the meta= argument from kwargs (if any)
    meta.update(
        {
            'filters': [],  # Applied filters as [(col, op, val), ...]
            'ignored': [],  # Ignored filters as [(col, vals), ...]
            'sort': [],  # Sorted columns as [(col, asc), ...]
            'offset': 0,  # Offset as integer
            'limit': None,  # Limit as integer - None if not applied
            'by': [],  # Group by columns as [col, ...]
        }
    )
    args = dict(args)  # Do not modify the args -- keep a copy
    controls = _pop_controls(args)
    transform = _transform_fn(transform, transform_kwargs)
    url, ext, query, queryfile, table, kwargs = _replace(
        engine, args, url, ext, query, queryfile, table, **kwargs
    )

    # Use the appropriate filter function based on the engine
    if engine == 'dataframe':
        data = transform(url) if callable(transform) else url
        return _filter_frame(data, meta, controls, args, argstype)
    elif engine == 'dir':
        data = dirstat(url, **args)
        data = transform(data) if callable(transform) else data
        return _filter_frame(data, meta, controls, args, argstype)
    elif engine in {'file', 'http', 'https'}:
        if engine == 'file' and not os.path.exists(url):
            raise OSError(f'url: {url} not found')
        # table= is not a valid option for all gramex.cache.open formats. Use only if specified
        if table is not None:
            kwargs['table'] = table
        # Get the full dataset. Then filter it
        data = gramex.cache.open(url, ext, transform=transform, **kwargs)
        return _filter_frame(data, meta, controls, args, argstype)
    elif engine.startswith('plugin+'):
        plugin = engine.split('+')[1]
        method = plugins[plugin]['filter']
        return method(
            url=url,
            meta=meta,
            controls=controls,
            args=args,
            argstype=argstype,
            id=id,
            table=table,
            columns=columns,
            ext=ext,
            query=query,
            queryfile=queryfile,
            **kwargs,
        )
    elif engine == 'sqlalchemy':
        state = kwargs.pop('state', None)
        engine = alter(url, table, columns, **kwargs)
        if query or queryfile:
            if queryfile:
                query = gramex.cache.open(queryfile, 'text')
            if not state:
                if isinstance(table, str):
                    state = table if ' ' in table else [table]
                elif isinstance(table, (list, tuple)):
                    state = list(table)
                elif table is not None:
                    raise ValueError(f'table: must be string or list of strings, not {table!r}')
            # sa.text() provides backend-neutral :name for bind parameters
            # NOTE: sa.text() caches queries => .bindparams() UPDATES previous bindparams. So:
            #   If query = "SELECT * FROM table WHERE x=:x" and we bind with bindparam('x'),
            #   we can NEVER bind with x as bindparam('x', expanding=True) without restarting.
            #   Therefore, create a new query each time.
            sql = sa.text(f'{query}; -- {time.time()}')
            all_params = {}
            for key, vals in args.items():
                conv, expanding = _argstype(argstype, key, str)
                if expanding:
                    all_params[key] = tuple(conv(val) for val in vals if val)
                    sql = sql.bindparams(sa.bindparam(key, expanding=True))
                elif len(vals) > 0:
                    all_params[key] = conv(vals[0])
            data = gramex.cache.query(sql, engine, state, params=all_params)
            data = transform(data) if callable(transform) else data
            # The query acts as base data. Now filter with additional parameters
            return _filter_frame(data, meta, controls, args, argstype)
        elif table:
            if callable(transform):
                data = gramex.cache.query(table, engine, [table])
                return _filter_frame(transform(data), meta, controls, args, argstype)
            else:
                return _filter_db(engine, table, meta, controls, args, argstype)
        else:
            raise ValueError('No table: or query: specified')
    else:
        raise ValueError(f'engine: {engine} invalid. Can be sqlalchemy|file|dataframe')

delete(url, args={}, meta={}, engine=None, table=None, ext=None, id=None, columns=None, query=None, queryfile=None, transform=None, transform_kwargs={}, argstype={}, kwargs)

Deletes data using URL query parameters.

Examples:

>>> gramex.data.delete(dataframe, args={'city': ['Oslo']})
>>> gramex.data.delete('mysql://server/db', table='x', args={'city': ['Oslo']})
>>> gramex.data.delete(url, args=handler.args)

It accepts the same parameters as gramex.data.filter, and returns the number of deleted rows.

Source code in gramex\data.py
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
def delete(
    url: Union[str, pd.DataFrame],
    args: dict = {},
    meta: dict = {},
    engine: str = None,
    table: str = None,
    ext: str = None,
    id: List[str] = None,
    columns: Dict[str, Union[str, dict]] = None,
    query: str = None,
    queryfile: str = None,
    transform: Callable = None,
    transform_kwargs: dict = {},
    argstype: Dict[str, dict] = {},
    **kwargs: dict,
) -> int:
    '''Deletes data using URL query parameters.

    Examples:
        >>> gramex.data.delete(dataframe, args={'city': ['Oslo']})
        >>> gramex.data.delete('mysql://server/db', table='x', args={'city': ['Oslo']})
        >>> gramex.data.delete(url, args=handler.args)

    It accepts the same parameters as [gramex.data.filter][], and returns the number
    of deleted rows.
    '''
    if engine is None:
        engine = get_engine(url)
    meta.update({'filters': [], 'ignored': []})
    args = dict(args)  # Do not modify the args -- keep a copy
    controls = _pop_controls(args)
    url, table, ext, query, queryfile, kwargs = _replace(
        engine, args, url, table, ext, query, queryfile, **kwargs
    )
    if engine == 'dataframe':
        data_filtered = _filter_frame(url, meta, controls, args, argstype, source='delete', id=id)
        return len(data_filtered)
    elif engine == 'file':
        data = gramex.cache.open(url, ext, transform=transform, **kwargs)
        data_filtered = _filter_frame(data, meta, controls, args, argstype, source='delete', id=id)
        gramex.cache.save(data, url, ext, index=False, **kwargs)
        return len(data_filtered)
    elif engine.startswith('plugin+'):
        plugin = engine.split('+')[1]
        method = plugins[plugin]['delete']
        return method(
            url=url,
            meta=meta,
            controls=controls,
            args=args,
            argstype=argstype,
            id=id,
            table=table,
            columns=columns,
            ext=ext,
            query=query,
            queryfile=queryfile,
            **kwargs,
        )
    elif engine == 'sqlalchemy':
        if table is None:
            raise ValueError('No table: specified')
        engine = alter(url, table, columns, **kwargs)
        return _filter_db(engine, table, meta, controls, args, argstype, source='delete', id=id)
    else:
        raise ValueError(f'engine: {engine} invalid. Can be sqlalchemy|file|dataframe')

update(url, args={}, meta={}, engine=None, table=None, ext=None, id=None, columns=None, query=None, queryfile=None, transform=None, transform_kwargs={}, argstype={}, kwargs)

Update data using URL query parameters.

Examples:

>>> gramex.data.update(dataframe, args=handler.args, id=['id'])
>>> gramex.data.update('file.csv', args=handler.args, id=['id'])
>>> gramex.data.update('mysql://server/db', table='x', args=handler.args, id=['id'])

id is a list of column names defining the primary key. Calling this in a handler with ?id=1&x=2 updates x=2 where id=1.

It accepts the same parameters as gramex.data.filter, and returns the number of updated rows.

Source code in gramex\data.py
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
def update(
    url: Union[str, pd.DataFrame],
    args: dict = {},
    meta: dict = {},
    engine: str = None,
    table: str = None,
    ext: str = None,
    id: List[str] = None,
    columns: Dict[str, Union[str, dict]] = None,
    query: str = None,
    queryfile: str = None,
    transform: Callable = None,
    transform_kwargs: dict = {},
    argstype: Dict[str, dict] = {},
    **kwargs: dict,
) -> int:
    '''Update data using URL query parameters.

    Examples:
        >>> gramex.data.update(dataframe, args=handler.args, id=['id'])
        >>> gramex.data.update('file.csv', args=handler.args, id=['id'])
        >>> gramex.data.update('mysql://server/db', table='x', args=handler.args, id=['id'])

    `id` is a list of column names defining the primary key.
    Calling this in a handler with `?id=1&x=2` updates x=2 where id=1.

    It accepts the same parameters as [gramex.data.filter][], and returns the number of updated
    rows.
    '''
    if engine is None:
        engine = get_engine(url)
    meta.update({'filters': [], 'ignored': []})
    args = dict(args)  # Do not modify the args -- keep a copy
    controls = _pop_controls(args)
    url, table, ext, query, queryfile, kwargs = _replace(
        engine, args, url, table, ext, query, queryfile, **kwargs
    )
    if engine == 'dataframe':
        data_updated = _filter_frame(url, meta, controls, args, argstype, source='update', id=id)
        return len(data_updated)
    elif engine == 'file':
        data = gramex.cache.open(url, ext, transform=transform, **kwargs)
        data_updated = _filter_frame(data, meta, controls, args, argstype, source='update', id=id)
        gramex.cache.save(data, url, ext, index=False, **kwargs)
        return len(data_updated)
    elif engine.startswith('plugin+'):
        plugin = engine.split('+')[1]
        method = plugins[plugin]['update']
        return method(
            url=url,
            meta=meta,
            controls=controls,
            args=args,
            argstype=argstype,
            id=id,
            table=table,
            columns=columns,
            ext=ext,
            query=query,
            queryfile=queryfile,
            **kwargs,
        )
    elif engine == 'sqlalchemy':
        if table is None:
            raise ValueError('No table: specified')
        engine = alter(url, table, columns, **kwargs)
        return _filter_db(engine, table, meta, controls, args, argstype, source='update', id=id)
    else:
        raise ValueError(f'engine: {engine} invalid. Can be sqlalchemy|file|dataframe')

insert(url, args={}, meta={}, engine=None, table=None, ext=None, id=None, columns=None, query=None, queryfile=None, transform=None, transform_kwargs={}, argstype={}, kwargs)

Insert data using URL query parameters.

Examples:

>>> gramex.data.insert(dataframe, args=handler.args, id=['id'])
>>> gramex.data.insert('file.csv', args=handler.args, id=['id'])
>>> gramex.data.insert('mysql://server/db', table='x', args=handler.args, id=['id'])

id is a list of column names defining the primary key. Calling this in a handler with ?id=3&x=2 inserts a new record with id=3 and x=2.

If the target file / table does not exist, it is created.

It accepts the same parameters as gramex.data.filter, and returns the number of updated rows.

Source code in gramex\data.py
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
def insert(
    url: Union[str, pd.DataFrame],
    args: dict = {},
    meta: dict = {},
    engine: str = None,
    table: str = None,
    ext: str = None,
    id: List[str] = None,
    columns: Dict[str, Union[str, dict]] = None,
    query: str = None,
    queryfile: str = None,
    transform: Callable = None,
    transform_kwargs: dict = {},
    argstype: Dict[str, dict] = {},
    **kwargs: dict,
) -> int:
    '''Insert data using URL query parameters.

    Examples:
        >>> gramex.data.insert(dataframe, args=handler.args, id=['id'])
        >>> gramex.data.insert('file.csv', args=handler.args, id=['id'])
        >>> gramex.data.insert('mysql://server/db', table='x', args=handler.args, id=['id'])

    `id` is a list of column names defining the primary key.
    Calling this in a handler with `?id=3&x=2` inserts a new record with id=3 and x=2.

    If the target file / table does not exist, it is created.

    It accepts the same parameters as [gramex.data.filter][], and returns the number of updated
    rows.
    '''
    if engine is None:
        engine = get_engine(url)
    args = dict(args)  # Do not modify the args -- keep a copy
    controls = _pop_controls(args)
    if not args:
        raise ValueError('No args: specified')
    meta.update({'filters': [], 'ignored': [], 'inserted': []})
    # If values do not have equal number of elements, pad them and warn
    rowcount = max(len(val) for val in args.values())
    for key, val in args.items():
        rows = len(val)
        if 0 < rows < rowcount:
            val += [val[-1]] * (rowcount - rows)
            app_log.warning(
                f'data.insert: column {key} has {rows} rows not {rowcount}. '
                f'Extended last value {val[-1]}'
            )
    rows = pd.DataFrame.from_dict(args)
    url, table, ext, query, queryfile, kwargs = _replace(
        engine, args, url, table, ext, query, queryfile, **kwargs
    )
    if engine == 'dataframe':
        rows = _pop_columns(rows, url.columns, meta['ignored'])
        url = url.append(rows, sort=False)
        return len(rows)
    elif engine == 'file':
        try:
            data = gramex.cache.open(url, ext, transform=None, **kwargs)
        except OSError:
            data = rows
        else:
            rows = _pop_columns(rows, data.columns, meta['ignored'])
            data = data.append(rows, sort=False)
        gramex.cache.save(data, url, ext, index=False, **kwargs)
        return len(rows)
    elif engine.startswith('plugin+'):
        plugin = engine.split('+')[1]
        method = plugins[plugin]['insert']
        return method(
            url=url,
            meta=meta,
            controls=controls,
            rows=rows,
            args=args,
            argstype=argstype,
            id=id,
            table=table,
            columns=columns,
            ext=ext,
            query=query,
            queryfile=queryfile,
            **kwargs,
        )
    elif engine == 'sqlalchemy':
        if table is None:
            raise ValueError('No table: specified')
        engine = alter(url, table, columns, **kwargs)
        try:
            cols = get_table(engine, table).columns
        except sa.exc.NoSuchTableError:
            pass
        else:
            rows = _pop_columns(rows, [col.name for col in cols], meta['ignored'])
        if '.' in table:
            kwargs['schema'], table = table.rsplit('.', 1)
        # SQLAlchemy 1.4+ only allows sa.inspect() for all table introspection
        if version.parse(sa.__version__) >= version.parse('1.4'):
            has_table = sa.inspect(engine).has_table(table, schema=kwargs.get('schema'))
        # SQLAlchemy 1.3- does not have .has_table()
        else:
            has_table = engine.dialect.has_table(engine, table, schema=kwargs.get('schema'))
        # If the DB doesn't yet have the table, create it WITH THE PRIMARY KEYS.
        if not has_table and id:
            # Note: pandas does not document get_schema, so it might change.
            engine.execute(pd.io.sql.get_schema(rows, name=table, keys=id, con=engine))

        def insert_method(tbl, conn, keys, data_iter):
            '''Pandas .to_sql() doesn't return inserted row primary keys. Capture it in meta'''
            data = [dict(zip(keys, row)) for row in data_iter]
            # If the ?id= is not provided, Pandas creates a schema based on available columns,
            # without the `id` column. SQLAlchemy won't return inserted_primary_key unless the
            # metadata has a primary key. So, hoping that the table already has a primary key,
            # load table from DB via extend_existing=True.
            sa_table = sa.Table(
                table, tbl.table.metadata, extend_existing=True, autoload_with=engine
            )
            r = conn.execute(sa_table.insert(), data)
            # SQLAlchemy 1.4+ supports inserted_primary_key_rows.
            if hasattr(r, 'inserted_primary_key_rows'):
                ids = r.inserted_primary_key_rows
            # In SQLAlchemy 1.3, only single inserts have an inserted_primary_key.
            elif hasattr(r, 'inserted_primary_key'):
                ids = [r.inserted_primary_key]
            else:
                ids = []
            # Add non-empty IDs as a dict with associated keys.
            # If there are no auto-generated primary keys in the table, no need to return anything.
            id_cols = [col.name for col in sa_table.primary_key]
            for row in ids:
                if row:
                    meta['inserted'].append(dict(zip(id_cols, row)))

        kwargs['method'] = insert_method
        # If user passes ?col= with an empty string, replace with NULL;
        # because, if the column is an INT/FLOAT, type conversion int('') / float('') will fail.
        for col in rows.columns:
            if rows[col].dtype == object:
                rows[col].replace({'': None}, inplace=True)

        # kwargs might contain additonal unexpected values, pass expected arguments explicitly
        pd.io.sql.to_sql(
            rows,
            table,
            engine,
            if_exists='append',
            index=False,
            schema=kwargs.get('schema', None),
            index_label=kwargs.get('index_label', None),
            chunksize=kwargs.get('chunksize', None),
            dtype=kwargs.get('dtype', None),
            method=kwargs.get('method', None),
        )

        return len(rows)
    else:
        raise ValueError(f'engine: {engine} invalid. Can be sqlalchemy|file|dataframe')

get_engine(url)

Detect the type of url passed.

The return value is

  • 'dataframe' if url is a Pandas DataFrame
  • 'sqlalchemy' if url is a sqlalchemy compatible URL
  • 'plugin' if it is <valid-plugin-name>://...
  • protocol if url is of the form protocol://...
  • 'dir' if it is not a URL but a valid directory
  • 'file' if it is not a URL but a valid file
  • None otherwise
Source code in gramex\data.py
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
def get_engine(url: Union[str, pd.DataFrame]) -> str:
    '''Detect the type of url passed.

    The return value is

    - `'dataframe'` if url is a Pandas DataFrame
    - `'sqlalchemy'` if url is a sqlalchemy compatible URL
    - `'plugin'` if it is `<valid-plugin-name>://...`
    - `protocol` if url is of the form `protocol://...`
    - `'dir'` if it is not a URL but a valid directory
    - `'file'` if it is not a URL but a valid file
    - `None` otherwise
    '''
    if isinstance(url, pd.DataFrame):
        return 'dataframe'
    for plugin_name in plugins:
        if url.startswith(f'{plugin_name}:'):
            return f'plugin+{plugin_name}'
    try:
        url = sa.engine.url.make_url(url)
    except sa.exc.ArgumentError:
        return 'dir' if os.path.isdir(url) else 'file'
    try:
        url.get_driver_name()
        return 'sqlalchemy'
    except sa.exc.NoSuchModuleError:
        return url.drivername

create_engine(url, create=sa.create_engine, kwargs)

Cached version of sqlalchemy.create_engine (or any custom engine).

Normally, this is not required. But gramex.data.get_table caches the engine and metadata and uses autoload=True. This makes sqlalchemy create a new database connection for every engine object, and not dispose it. So we re-use the engine objects within this module.

Source code in gramex\data.py
650
651
652
653
654
655
656
657
658
659
660
661
def create_engine(url: str, create: sa.engine.base.Engine = sa.create_engine, **kwargs: dict):
    '''
    Cached version of sqlalchemy.create_engine (or any custom engine).

    Normally, this is not required. But [gramex.data.get_table][] caches the engine
    *and* metadata *and* uses autoload=True. This makes sqlalchemy create a new
    database connection for every engine object, and not dispose it. So we
    re-use the engine objects within this module.
    '''
    if url not in _ENGINE_CACHE:
        _ENGINE_CACHE[url] = create(url, **kwargs)
    return _ENGINE_CACHE[url]

get_table(engine, table, kwargs)

Return the sqlalchemy table from the engine and table name

Source code in gramex\data.py
664
665
666
667
668
669
670
671
def get_table(engine: sa.engine.base.Engine, table: str, **kwargs: dict) -> sa.Table:
    '''Return the sqlalchemy table from the engine and table name'''
    if engine not in _METADATA_CACHE:
        _METADATA_CACHE[engine] = sa.MetaData(bind=engine)
    metadata = _METADATA_CACHE[engine]
    if '.' in table:
        kwargs['schema'], table = table.rsplit('.', 1)
    return sa.Table(table, metadata, autoload=True, autoload_with=engine, **kwargs)

download(data, format='json', template=None, args={}, kwargs)

Download a DataFrame or dict of DataFrames in various formats. This is used by gramex.handlers.FormHandler. You are strongly advised to try it before creating your own FunctionHandler.

Usage as a FunctionHandler

def download_as_csv(handler): handler.set_header(‘Content-Type’, ‘text/csv’) handler.set_header(‘Content-Disposition’, ‘attachment;filename=data.csv’) return gramex.data.download(dataframe, format=’csv’)

It takes the following arguments:

Parameters:

Name Type Description Default
data Union[pd.DataFrame, List[pd.DataFrame]]

A DataFrame or a dict of DataFrames

required
format str

Output format. Can be csv|json|html|xlsx|template

'json'
template str

Path to template file for template format

None
args dict

dictionary of user arguments to subsitute spec

{}
**kwargs dict

Additional parameters that are passed to the relevant renderer

{}

Returns:

Type Description
bytes

bytes with the download file contents

When data is a DataFrame, this is what different format= parameters return:

  • csv returns a UTF-8-BOM encoded CSV file of the dataframe
  • xlsx returns an Excel file with 1 sheet named data. kwargs are passed to .to_excel(index=False)
  • html returns a HTML file with a single table. kwargs are passed to .to_html(index=False)
  • json returns a JSON file. kwargs are passed to .to_json(orient='records', force_ascii=True).
  • template returns a Tornado template rendered file. The template receives data as data and any additional kwargs.
  • pptx returns a PPTX generated by pptgen
  • seaborn or sns returns a Seaborn generated chart
  • vega returns JavaScript that renders a Vega chart

When data is a dict of DataFrames, the following additionally happens:

  • format='csv' renders all DataFrames one below the other, adding the key as heading
  • format='xlsx' renders each DataFrame on a sheet whose name is the key
  • format='html' renders tables below one another with the key as heading
  • format='json' renders as a dict of DataFrame JSONs
  • format='template' sends data and all kwargs as passed to the template
  • format='pptx' passes data as a dict of datasets to pptgen
  • format='vega' passes data as a dict of datasets to Vega

When data is NEITHER a DataFrame or a dict of DataFrames:

  • format='json' renders as JSON if possible
  • format='template' renders the template if possible
  • all other formats raise a ValueError

You need to set the MIME types on the handler yourself. Recommended MIME types are in gramex.yaml under handler.FormHandler.

Source code in gramex\data.py
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
def download(
    data: Union[pd.DataFrame, List[pd.DataFrame]],
    format: str = 'json',
    template: str = None,
    args: dict = {},
    **kwargs: dict,
) -> bytes:
    '''
    Download a DataFrame or dict of DataFrames in various formats. This is used
    by [gramex.handlers.FormHandler][]. You are **strongly** advised to
    try it before creating your own FunctionHandler.

    Usage as a FunctionHandler:

        def download_as_csv(handler):
            handler.set_header('Content-Type', 'text/csv')
            handler.set_header('Content-Disposition', 'attachment;filename=data.csv')
            return gramex.data.download(dataframe, format='csv')

    It takes the following arguments:

    Parameters:
        data: A DataFrame or a dict of DataFrames
        format: Output format. Can be `csv|json|html|xlsx|template`
        template: Path to template file for `template` format
        args: dictionary of user arguments to subsitute spec
        **kwargs: Additional parameters that are passed to the relevant renderer

    Returns:
        bytes with the download file contents

    When `data` is a DataFrame, this is what different `format=` parameters
    return:

    - `csv` returns a UTF-8-BOM encoded CSV file of the dataframe
    - `xlsx` returns an Excel file with 1 sheet named `data`. kwargs are
        passed to `.to_excel(index=False)`
    - `html` returns a HTML file with a single table. kwargs are passed to
        `.to_html(index=False)`
    - `json` returns a JSON file. kwargs are passed to
        `.to_json(orient='records', force_ascii=True)`.
    - `template` returns a Tornado template rendered file. The template
        receives `data` as `data` and any additional kwargs.
    - `pptx` returns a PPTX generated by pptgen
    - `seaborn` or `sns` returns a Seaborn generated chart
    - `vega` returns JavaScript that renders a Vega chart

    When `data` is a dict of DataFrames, the following additionally happens:

    - `format='csv'` renders all DataFrames one below the other, adding the
        key as heading
    - `format='xlsx'` renders each DataFrame on a sheet whose name is the key
    - `format='html'` renders tables below one another with the key as heading
    - `format='json'` renders as a dict of DataFrame JSONs
    - `format='template'` sends `data` and all `kwargs` as passed to the
        template
    - `format='pptx'` passes `data` as a dict of datasets to pptgen
    - `format='vega'` passes `data` as a dict of datasets to Vega

    When `data` is NEITHER a DataFrame or a dict of DataFrames:

    - `format='json'` renders as JSON if possible
    - `format='template'` renders the template if possible
    - all other formats raise a `ValueError`

    You need to set the MIME types on the handler yourself. Recommended MIME
    types are in gramex.yaml under handler.FormHandler.
    '''
    multiple_datasets = True
    error_no_dataframe = None

    # Check if data is a DataFrame or a dict of DataFrames (multiple_datasets).
    # Ensure that data becomes a dict of DataFrames
    if isinstance(data, dict):
        for key, val in data.items():
            if not isinstance(val, pd.DataFrame):
                error_no_dataframe = f'download(): {key} type is {type(val)}, not a DataFrame'
        if not len(data):
            error_no_dataframe = 'download(): got empty dict. Need a DataFrame'
    elif not isinstance(data, pd.DataFrame):
        error_no_dataframe = f'download(): type is {type(data)}, not a DataFrame'
    else:
        data = {'data': data}
        multiple_datasets = False

    # These formats require a DataFrame or a dict of DataFrames. Other formats (json, template)
    # accept anything.
    if error_no_dataframe and format in {
        'csv',
        'html',
        'xlsx',
        'xls',
        'pptx',
        'ppt',
        'seaborn',
        'sns',
        'vega',
        'vega-lite',
        'vegam',
    }:
        raise ValueError(error_no_dataframe)

    def kw(**conf):
        '''Set provided conf as defaults for kwargs'''
        return merge(kwargs, conf, mode='setdefault')

    if format == 'csv':
        # csv.writer requires BytesIO in PY2 and StringIO in PY3.
        # I can't see an elegant way out of this other than writing code for each.
        out = io.StringIO()
        kw(index=False)
        for index, (key, val) in enumerate(data.items()):
            if index > 0:
                out.write('\n')
            if multiple_datasets:
                out.write(key + '\n')
            val.to_csv(out, **kwargs)
        result = out.getvalue()
        # utf-8-sig encoding returns the result with a UTF-8 BOM. Easier to open in Excel
        return result.encode('utf-8-sig') if result.strip() else result.encode('utf-8')
    elif format == 'template':
        return gramex.cache.open(template, 'template').generate(
            data=data if multiple_datasets else data['data'], **kwargs
        )
    elif format == 'html':
        out = io.StringIO()
        kw(index=False)
        for key, val in data.items():
            if multiple_datasets:
                out.write(f'<h1>{key}</h1>')
            val.to_html(out, **kwargs)
        return out.getvalue().encode('utf-8')
    elif format in {'xlsx', 'xls'}:
        out = io.BytesIO()
        kw(index=False)
        # TODO: Create and use a FrameWriter for formatting
        with pd.ExcelWriter(out, engine='xlsxwriter') as writer:
            for key, val in data.items():
                val.to_excel(writer, sheet_name=key, **kwargs)
        return out.getvalue()
    elif format in {'pptx', 'ppt'}:
        from gramex.pptgen import pptgen

        kw()
        out = io.BytesIO()
        pptgen(target=out, data=data, **kwargs)
        return out.getvalue()
    elif format in {'seaborn', 'sns'}:
        kw = AttrDict()
        defaults = {
            'chart': 'barplot',
            'ext': 'png',
            'data': 'data',
            'dpi': 96,
            'width': 640,
            'height': 480,
        }
        for key, default in defaults.items():
            kw[key] = kwargs.pop(key, default)
        import matplotlib

        matplotlib.use('Agg')  # Before importing seaborn, set a headless backend
        import seaborn as sns

        plot = getattr(sns, kw.chart)(data=data.get(kw.data), **kwargs)
        out = io.BytesIO()
        fig = plot.figure if hasattr(plot, 'figure') else plot.fig
        for k in ['dpi', 'width', 'height']:
            kw[k] = float(kw[k])
        fig.set_size_inches(kw.width / kw.dpi, kw.height / kw.dpi)
        fig.savefig(out, format=kw.ext, dpi=kw.dpi)
        fig.clear()
        return out.getvalue()
    elif format in {'vega', 'vega-lite', 'vegam'}:
        kwargs = kw(orient='records', force_ascii=True)
        spec = kwargs.pop('spec', {})
        kwargs.pop('handler', None)
        out = io.BytesIO()
        # conf = {..., spec: {..., data: __DATA__}}
        if isinstance(spec.get('data'), (dict, list)) or 'fromjson' in spec:
            # support only one dataset
            values = list(data.values())
            out.write(values[0].to_json(**kwargs).encode('utf-8'))
            out = out.getvalue()
        else:
            spec['data'] = '__DATA__'
            for index, (key, val) in enumerate(data.items()):
                out.write(b',{"name":' if index > 0 else b'{"name":')
                out.write(json_encode(key).encode('utf-8'))
                out.write(b',"values":')
                out.write(val.to_json(**kwargs).encode('utf-8'))
                out.write(b'}')
            out = out.getvalue()
            if format == 'vega':
                out = b'[' + out + b']'
        kwargs['spec'], _ = _replace('', args, spec)
        conf = json.dumps(kwargs, ensure_ascii=True, separators=(',', ':'), indent=None)
        conf = conf.encode('utf-8').replace(b'"__DATA__"', out)
        script = gramex.cache.open(_VEGA_SCRIPT, 'bin')
        return script.replace(b'/*{conf}*/', conf)
    # If it's none of these formats, default to JSON.
    # If there are no DataFrames, handle arbitrary JSON
    elif error_no_dataframe:
        from gramex.config import CustomJSONEncoder

        kw(cls=CustomJSONEncoder)
        return json.dumps(data, **kwargs)
    # If there ARE DataFrames, render each
    else:
        out = io.BytesIO()
        kwargs = kw(orient='records', force_ascii=True)
        if multiple_datasets:
            out.write(b'{')
            for index, (key, val) in enumerate(data.items()):
                if index > 0:
                    out.write(b',')
                out.write(json_encode(key).encode('utf-8'))
                out.write(b':')
                out.write(val.to_json(**kwargs).encode('utf-8'))
            out.write(b'}')
        else:
            out.write(data['data'].to_json(**kwargs).encode('utf-8'))
        return out.getvalue()

dirstat(url, timeout=10, kwargs)

Return a DataFrame with the list of all files & directories under the url.

Parameters:

Name Type Description Default
url str

path to a directory, or a URL like dir:///c:/path/, dir:////root/dir/

required
timeout int

max seconds to wait. None to wait forever

10

Raises:

Type Description
OSError

if url points to a missing location or is not a directory.

Returns:

Type Description
pd.DataFrame

DataFrame with 1 row per file/directory in the URL

The result has these columns.

  • type: extension with a . prefix – or dir
  • dir: directory path to the file relative to the URL
  • name: file name (including extension)
  • path: full path to file or dir. This equals url / dir / name
  • size: file size
  • mtime: last modified time in seconds since epoch
  • level: path depth (i.e. the number of paths in dir)
Source code in gramex\data.py
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
def dirstat(url: str, timeout: int = 10, **kwargs: dict) -> pd.DataFrame:
    '''Return a DataFrame with the list of all files & directories under the url.

    Parameters:
        url: path to a directory, or a URL like `dir:///c:/path/`, `dir:////root/dir/`
        timeout: max seconds to wait. `None` to wait forever

    Raises:
        OSError: if url points to a missing location or is not a directory.

    Returns:
        DataFrame with 1 row per file/directory in the URL

    The result has these columns.

    - `type`: extension with a `.` prefix -- or `dir`
    - `dir`: directory path to the file relative to the URL
    - `name`: file name (including extension)
    - `path`: full path to file or dir. This equals url / dir / name
    - `size`: file size
    - `mtime`: last modified time in seconds since epoch
    - `level`: path depth (i.e. the number of paths in dir)
    '''
    try:
        url = sa.engine.url.make_url(url)
        target = url.database
    except sa.exc.ArgumentError:
        target = url
    if not os.path.isdir(target):
        raise OSError(f'dirstat: {target} is not a directory')
    target = os.path.normpath(target)
    result = []
    start_time = time.time()
    for dirpath, dirnames, filenames in os.walk(target):
        if timeout and time.time() - start_time > timeout:
            app_log.debug(f'dirstat: {url} timeout ({timeout:.1f}s)')
            break
        for name in dirnames:
            path = os.path.join(dirpath, name)
            stat = os.stat(path)
            dirname = dirpath.replace(target, '').replace(os.sep, '/') + '/'
            result.append(
                {
                    'path': path,
                    'dir': dirname,
                    'name': name,
                    'type': 'dir',
                    'size': stat.st_size,
                    'mtime': stat.st_mtime,
                    'level': dirname.count('/'),
                }
            )
        for name in filenames:
            path = os.path.join(dirpath, name)
            stat = os.stat(path)
            dirname = dirpath.replace(target, '').replace(os.sep, '/') + '/'
            result.append(
                {
                    'path': path,
                    'dir': dirname,
                    'name': name,
                    'type': os.path.splitext(name)[-1],
                    'size': stat.st_size,
                    'mtime': stat.st_mtime,
                    'level': dirname.count('/'),
                }
            )
    return pd.DataFrame(result)

filtercols(url, args={}, meta={}, engine=None, ext=None, query=None, queryfile=None, transform=None, transform_kwargs={}, separator=',', in_memory=False, kwargs)

Filter data and extract unique values of each column using URL query parameters.

Examples:

>>> gramex.data.filtercols(dataframe, args=handler.args)
>>> gramex.data.filtercols('file.csv', args=handler.args)
>>> gramex.data.filtercols('mysql://server/db', table='table', args=handler.args)

Parameters:

Name Type Description Default
url Union[str, pd.DataFrame]

Pandas DataFrame, sqlalchemy URL, directory or file name, .format-ed using args.

required
args dict

URL query parameters as a dict of lists. Pass handler.args or parse_qs results

{}
meta dict

this dict is updated with metadata during the course of filtering

{}
engine str

over-rides the auto-detected engine. Can be ‘dataframe’, ‘file’, ‘http’, ‘https’, ‘sqlalchemy’, ‘dir’

None
ext str

file extension (if url is a file). Defaults to url extension

None
query str

optional SQL query to execute (if url is a database), .format-ed using args and supports SQLAlchemy SQL parameters. Loads entire result in memory before filtering.

None
queryfile str

optional SQL query file to execute (if url is a database). Same as specifying the query: in a file. Overrides query:

None
transform function

optional in-memory transform of source data. Takes the result of gramex.cache.open or gramex.cache.query. Must return a DataFrame. Applied to both file and SQLAlchemy urls.

None
transform_kwargs dict

optional keyword arguments to be passed to the transform function – apart from data

{}
separator str

string that separates columns in a hierarchy. Defaults to ,. For example, ?_c=a,b treats columns a and b as a tuple / hierarchy and filters them together.

','
in_memory bool

fetch all unique values and compute filters in-memory. Faster, but takes more memory.

False
**kwargs dict

Additional parameters are passed to gramex.cache.open or sqlalchemy.create_engine

{}

Returns:

Type Description
pd.DataFrame

filtered DataFrame

Remaining kwargs are passed to gramex.cache.open if url is a file, or sqlalchemy.create_engine if url is a SQLAlchemy URL.

If this is used in a handler as

>>> filtered = gramex.data.filtercols(dataframe, args=handler.args)

… then calling the handler with ?_c=state&_c=district returns all unique values in columns of dataframe where columns are state and district.

Column filter is supported like this:

  • ?_c=y&x returns df with unique values of y where x is not null
  • ?_c=y&x=val returns df with unique values of y where x == val
  • ?_c=y&y=val returns df with unique values of y, ignores filter y == val
  • ?_c=y&x>=val returns df with unique values of y where x > val
  • ?_c=x&_c=y&x=val returns df with unique values of x ignoring filter x == val and returns unique values of y where x == val

Arguments are converted to the type of the column before comparing. If this fails, it raises a ValueError.

These URL query parameters control the output:

  • ?_sort=col sorts column col in ascending order. ?_sort=-col sorts in descending order.
  • ?_limit=100 limits the result to 100 rows
  • ?_offset=100 starts showing the result from row 100. Default: 0
  • ?_c=x&_c=y returns only columns [x, y]. ?_c=-col drops col.

If a column name matches one of the above, you cannot filter by that column. Avoid column names beginning with _.

You can handle hierarchies by passing ?_c=Country,City with a comma. This returns all unique combinations of Country and City.

To get the min/max or a column, use aggregations, e.g. ?_c=age|min&_c=age|max. You can use ?_c=age|range as a shortcut that returns min and max of a column.

To get additional information about the filtering, use:

meta = {}      # Create a variable which will be filled with more info
filtered = gramex.data.filter(data, meta=meta, **handler.args)

The meta variable is populated with the following keys:

  • filters: Applied filters as [(col, op, val), ...]
  • ignored: Ignored filters as [(col, vals), ('_sort', cols), ...]
  • excluded: Excluded columns as [col, ...]
  • sort: Sorted columns as [(col, True), ...]. The second parameter is ascending=
  • offset: Offset as integer. Defaults to 0
  • limit: Limit as integer - 100 if limit is not applied
  • count: Total number of rows, if available

These variables may be useful to show additional information about the filtered data.

Source code in gramex\data.py
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
def filtercols(
    url: Union[str, pd.DataFrame],
    args: dict = {},
    meta: dict = {},
    engine: str = None,
    ext: str = None,
    query: str = None,
    queryfile: str = None,
    transform: Callable = None,
    transform_kwargs: dict = {},
    separator: str = ',',
    in_memory: bool = False,
    **kwargs: dict,
) -> pd.DataFrame:
    '''Filter data and extract unique values of each column using URL query parameters.

    Examples:
        >>> gramex.data.filtercols(dataframe, args=handler.args)
        >>> gramex.data.filtercols('file.csv', args=handler.args)
        >>> gramex.data.filtercols('mysql://server/db', table='table', args=handler.args)

    Parameters:

        url: Pandas DataFrame, sqlalchemy URL, directory or file name,
            `.format`-ed using `args`.
        args: URL query parameters as a dict of lists. Pass handler.args or parse_qs results
        meta: this dict is updated with metadata during the course of filtering
        engine: over-rides the auto-detected engine. Can be 'dataframe', 'file',
            'http', 'https', 'sqlalchemy', 'dir'
        ext: file extension (if url is a file). Defaults to url extension
        query: optional SQL query to execute (if url is a database),
            `.format`-ed using `args` and supports SQLAlchemy SQL parameters.
            Loads entire result in memory before filtering.
        queryfile: optional SQL query file to execute (if url is a database).
            Same as specifying the `query:` in a file. Overrides `query:`
        transform (function): optional in-memory transform of source data. Takes
            the result of gramex.cache.open or gramex.cache.query. Must return a
            DataFrame. Applied to both file and SQLAlchemy urls.
        transform_kwargs: optional keyword arguments to be passed to the
            transform function -- apart from data
        separator: string that separates columns in a hierarchy. Defaults to `,`.
            For example, `?_c=a,b` treats columns `a` and `b` as a tuple / hierarchy and
            filters them *together*.
        in_memory: fetch all unique values and compute filters in-memory. Faster,
            but takes more memory.
        **kwargs: Additional parameters are passed to
            [gramex.cache.open][] or `sqlalchemy.create_engine`

    Returns:
        filtered DataFrame

    Remaining kwargs are passed to [gramex.cache.open][] if `url` is a file, or
    `sqlalchemy.create_engine` if `url` is a SQLAlchemy URL.

    If this is used in a handler as

        >>> filtered = gramex.data.filtercols(dataframe, args=handler.args)

    ... then calling the handler with `?_c=state&_c=district` returns all unique values
    in columns of `dataframe` where columns are state and district.

    Column filter is supported like this:

    - `?_c=y&x` returns df with unique values of y where x is not null
    - `?_c=y&x=val` returns df with unique values of y where x == val
    - `?_c=y&y=val` returns df with unique values of y, ignores filter y == val
    - `?_c=y&x>=val` returns df with unique values of y where x > val
    - `?_c=x&_c=y&x=val` returns df with unique values of x ignoring filter x == val
        and returns unique values of y where x == val

    Arguments are converted to the type of the column before comparing. If this
    fails, it raises a ValueError.

    These URL query parameters control the output:

    - `?_sort=col` sorts column col in ascending order. `?_sort=-col` sorts
        in descending order.
    - `?_limit=100` limits the result to 100 rows
    - `?_offset=100` starts showing the result from row 100. Default: 0
    - `?_c=x&_c=y` returns only columns `[x, y]`. `?_c=-col` drops col.

    If a column name matches one of the above, you cannot filter by that column.
    Avoid column names beginning with _.

    You can handle hierarchies by passing `?_c=Country,City` with a comma. This returns all unique
    *combinations* of `Country` and `City`.

    To get the min/max or a column, use aggregations, e.g. `?_c=age|min&_c=age|max`.
    You can use `?_c=age|range` as a shortcut that returns min and max of a column.

    To get additional information about the filtering, use:

        meta = {}      # Create a variable which will be filled with more info
        filtered = gramex.data.filter(data, meta=meta, **handler.args)

    The `meta` variable is populated with the following keys:

    - `filters`: Applied filters as `[(col, op, val), ...]`
    - `ignored`: Ignored filters as `[(col, vals), ('_sort', cols), ...]`
    - `excluded`: Excluded columns as `[col, ...]`
    - `sort`: Sorted columns as `[(col, True), ...]`. The second parameter is `ascending=`
    - `offset`: Offset as integer. Defaults to 0
    - `limit`: Limit as integer - `100` if limit is not applied
    - `count`: Total number of rows, if available

    These variables may be useful to show additional information about the
    filtered data.
    '''
    # Auto-detect engine.
    if engine is None:
        engine = get_engine(url)
    result = {}
    limit = args.get('_limit', [100])
    try:
        limit = min(int(v) for v in limit)
    except ValueError:
        raise ValueError(f'_limit not integer: {limit!r}')
    if in_memory:
        # Fetch the superset data, i.e. all filter columns
        in_memory_args = {'_c': [], '_by': set()}
        for col in args.get('_c', []):
            name, agg = col.rsplit(_agg_sep, 1) if _agg_sep in col else (col, None)
            for c in name.split(separator):
                in_memory_args['_by'].add(c)
        # Apply all filters while fetching, if skip the filter columns.
        # We'll apply THOSE filters independently
        for key, vals in args.items():
            col = key
            for op in operators:
                if col.endswith(op):
                    col = col[: -len(op)]
                    break
            if _agg_sep in col:
                col = col.rsplit(_agg_sep, 1)[0]
            if col not in in_memory_args['_by']:
                in_memory_args[key] = vals
        url = filter(url, args=in_memory_args, **kwargs)

    # Get unique values for each column
    for col in args.get('_c', []):
        # If ?_c=sales|RANGE, get the range
        name, agg = col.rsplit(_agg_sep, 1) if _agg_sep in col else (col, None)
        # If ?_c=a,b then treat columns a and b as a pair
        cols = name.split(separator)
        # col_args takes _sort, _c and all filters from args
        col_args = {}
        for key, value in args.items():
            # Apply only _sort as a control. Ignore _by, _limit, _offset, etc.
            if key in ['_sort']:
                col_args[key] = value
            # Apply filters. But ignore filters on the columns we're currently processing
            if not key.startswith('_') and key not in cols:
                col_args[key] = value
        if agg:
            # Convert ?_c=col|RANGE to ?_c=col|MIN&_c=col|MAX
            aggs = ['min', 'max'] if agg.lower() == 'range' else [agg]
            # Group by all values, just return the aggregations
            col_args['_by'] = ['']
            col_args['_c'] = [f'{c}{_agg_sep}{a}' for c in cols for a in aggs]
        else:
            col_args['_by'] = cols
            col_args['_c'] = []
            col_args['_limit'] = [limit]
        result[col] = gramex.data.filter(url, args=col_args, **kwargs)
    return result

alter(url, table, columns=None, kwargs)

Create or alter a table with columns specified.

Examples:

>>> gramex.data.alter(url, table, columns={
...     'id': {'type': 'int', 'primary_key': True, 'autoincrement': True},
...     'when': {'type': 'timestamp', 'default': {'function': 'func.now()'}},
...     'email': {'nullable': True, 'default': 'none'},
...     'age': {'type': 'float', 'nullable': False, 'default': 18},
... })

Parameters:

Name Type Description Default
url str

sqlalchemy URL

required
table str

table name

required
columns Dict[str, Union[str, dict]]

column names, with values as SQL types or type objects

None
**kwargs dict

passed to sqlalchemy.create_engine().

{}

Returns:

Type Description
sa.engine.base.Engine

SQLAlchemy engine

If the table exists, new columns (if any) are added. Existing columns are NOT changed.

If the table does not exist, the table is created with the specified columns.

If there are no changes, the function returns quickly (5 ms), since metadata is cached.

columns can be a dict with values as SQL types (e.g. "INTEGER" or "VARCHAR(10)"):

>>> gramex.data.alter(url, table, columns={'id': 'INTEGER', 'name': 'VARCHAR(10)'})

… or a dict like {column_name: type, column_name: type, ...}:

>>> gramex.data.alter(url, table, columns={
...     'id': {'type': 'int', 'primary_key': True, 'autoincrement': True},
...     'when': {'type': 'timestamp', 'default': {'function': 'func.now()'}},
...     'email': {'nullable': True, 'default': 'none'},
...     'age': {'type': 'float', 'nullable': False, 'default': 18},
... })

If the columns values are a dict, these keys are allowed:

  • type (str): SQL type, e.g. "VARCHAR(10)"
  • default (str/int/float/bool/function/dict):
    • A scalar like "none@example.org" for fixed default values
    • A SQLAlchemy function like sqlalchemy.func.now()
    • A dict like {function: func.now()} containing a SQLAlchemy functions
  • nullable (bool): whether column can have null values, e.g. False
  • primary_key (bool): whether column is a primary key, e.g. True
  • autoincrement (bool): whether column automatically increments, e.g. True

primary_key and autoincrement are used only when creating new tables. They do not change existing primary keys or autoincrements. This is because SQLite disallows PRIMARY KEY with ALTER and AUTO_INCREMENT doesn’t work without PRIMARY KEY in MySQL.

Source code in gramex\data.py
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
def alter(
    url: str, table: str, columns: Dict[str, Union[str, dict]] = None, **kwargs: dict
) -> sa.engine.base.Engine:
    '''Create or alter a table with columns specified.

    Examples:
        >>> gramex.data.alter(url, table, columns={
        ...     'id': {'type': 'int', 'primary_key': True, 'autoincrement': True},
        ...     'when': {'type': 'timestamp', 'default': {'function': 'func.now()'}},
        ...     'email': {'nullable': True, 'default': 'none'},
        ...     'age': {'type': 'float', 'nullable': False, 'default': 18},
        ... })

    Parameters:
        url: sqlalchemy URL
        table: table name
        columns: column names, with values as SQL types or type objects
        **kwargs: passed to `sqlalchemy.create_engine()`.

    Returns:
        SQLAlchemy engine

    If the table exists, new columns (if any) are added. Existing columns are **NOT changed**.

    If the table does not exist, the table is created with the specified columns.

    If there are no changes, the function returns quickly (5 ms), since metadata is cached.

    `columns` can be a dict with values as SQL types (e.g. `"INTEGER"` or `"VARCHAR(10)"`):

        >>> gramex.data.alter(url, table, columns={'id': 'INTEGER', 'name': 'VARCHAR(10)'})

    ... or a dict like `{column_name: type, column_name: type, ...}`:

        >>> gramex.data.alter(url, table, columns={
        ...     'id': {'type': 'int', 'primary_key': True, 'autoincrement': True},
        ...     'when': {'type': 'timestamp', 'default': {'function': 'func.now()'}},
        ...     'email': {'nullable': True, 'default': 'none'},
        ...     'age': {'type': 'float', 'nullable': False, 'default': 18},
        ... })

    If the `columns` values are a dict, these keys are allowed:

    - `type` (str): SQL type, e.g. `"VARCHAR(10)"`
    - `default` (str/int/float/bool/function/dict):
        - A scalar like `"none@example.org"` for fixed default values
        - A SQLAlchemy function like `sqlalchemy.func.now()`
        - A dict like `{function: func.now()}` containing a SQLAlchemy functions
    - `nullable` (bool): whether column can have null values, e.g. `False`
    - `primary_key` (bool): whether column is a primary key, e.g. `True`
    - `autoincrement` (bool): whether column automatically increments, e.g. `True`

    `primary_key` and `autoincrement` are used **only** when creating new tables. They do not
    change existing primary keys or autoincrements. This is because
    [SQLite disallows PRIMARY KEY with ALTER](https://stackoverflow.com/a/1120030/100904)
    and AUTO_INCREMENT doesn't work without PRIMARY KEY in MySQL.
    '''
    engine = create_engine(url, **kwargs)
    if columns is None:
        return engine
    # alter is not required for schema-less databases. For now, hard-code engine names
    scheme = urlparse(url).scheme
    if scheme in {'mongodb', 'elasticsearch', 'influxdb'}:
        app_log.info(f'alter() not required for schema-less DB {engine.driver}')
        return engine
    try:
        db_table = get_table(engine, table, extend_existing=True)
    except sa.exc.NoSuchTableError:
        # If the table's not in the DB, create it
        cols = []
        for name, row in columns.items():
            row = dict({'type': row} if isinstance(row, str) else row, name=name)
            col_type = row.get('type', 'text')
            if isinstance(col_type, str):
                # Use eval() to handle direct types like INTEGER *and* expressions like VARCHAR(3)
                # eval() is safe here since `col_type` is written by app developer
                # B307:eval is safe here since `col_type` is written by app developer
                row['type'] = eval(col_type.upper(), vars(sa.types))  # nosec B307
            row['type_'] = row.pop('type')
            if 'default' in row:
                from inspect import isclass

                default = row.pop('default')
                # default: can be a string like `'sa.func.now()'` or `'func.now()'`
                if isinstance(default, dict):
                    libs = {'sa': sa, 'sqlalchemy': sa, 'func': sa.func}
                    row['server_default'] = build_transform(
                        default, vars={key: None for key in libs}, iter=False
                    )(**libs)
                # default can be an SQLAlchemy function, e.g. sa.func.now()
                elif isclass(default) and issubclass(default, sa.func.Function):
                    row['server_default'] = default
                # default can also be a static value, e.g. `0`
                else:
                    row['server_default'] = str(default)
            cols.append(sa.Column(**row))
        sa.Table(table, _METADATA_CACHE[engine], *cols, extend_existing=True).create(engine)
    else:
        quote = engine.dialect.identifier_preparer.quote_identifier
        # If the table's already in the DB, add new columns. We can't change column types
        with engine.connect() as conn, conn.begin():
            for name, row in columns.items():
                if name in db_table.columns:
                    continue
                row = {'type': row} if isinstance(row, str) else row
                col_type = row.get('type', 'text')
                constraints = []
                if 'nullable' in row:
                    constraints.append('' if row['nullable'] else 'NOT NULL')
                if 'default' in row:
                    # repr() converts int, float properly,
                    #   str into 'str' with single quotes (which is the MySQL standard)
                    #   TODO: datetime and other types will fail
                    if isinstance(row['default'], dict) or callable(row['default']):
                        app_log.warning(
                            f'alter(): col {name} cannot change default on existing table'
                        )
                    else:
                        constraints += ['DEFAULT', repr(row['default'])]
                # This syntax works on DB2, MySQL, Oracle, PostgreSQL, SQLite
                conn.execute(
                    f'ALTER TABLE {quote(table)} '
                    f'ADD COLUMN {quote(name)} {col_type} {" ".join(constraints)}'
                )
        # Refresh table metadata after altering
        get_table(engine, table, extend_existing=True)
    return engine