Cache and open files

The feature I use most often in Gramex 0.18 is It’s a simple replacement for reading CSV, XSLX files, etc. But it also caches.

For those familiar with Gramex 0.x, it’s exactly like DB.csv or

In fact, don’t ever use the regular open, or pd.read_csv. Always use It’s vastly better.

Read files

To read a CSV file, use just:

data ='data.csv', 'csv')

You can call this as often as you like. The DataFrame will be re-loaded only if the file is updated.

You can pass additional parameters to read_csv, like:

data ='data.csv', 'csv', encoding='utf-8')

File formats

The second parameter can be any of text, json, yaml, xlsx, etc. You can pass additional parameters to all of these. json and yaml use json.load and yaml.load. The rest use pandas.read_*

You can also use “markdown” as the second parameter. That converts Markdown to a HTML string.

Custom calculations

The second parameter can be any function that takes a filename (and any optional parameters) to return anything. You can put in calculations in here to return any value.

For example:

def compute(filename):
    data = pd.read_csv(filename)
    return {
        'columns': data.columns,
        'summary': data.groupby('category')['sales'].sum()

data ='data.csv', compute)

The first time, the result is the same as compute(‘data.csv’). After that, the result is cached. When data.csv is changed, compute is called again.

Global cache

The data is cached globally across the Gramex instance. You can reload this from across different functions or modules. The cache still remains.

Always use it

Like I mentioned: there’s no reason NOT to use Replace all file open methods with this.