Web2Py’s SQLFORM.grid is a very powerful plugin for managing complex CRUD operations. It provides several operations like create, update, delete, search, sort and browser records. It also gives pagination for large records. It is well explained here.
Export functionality
- The export is one of the most useful functionalities of the SQLFORM.grid.
- You can easily export your data in various format like CSV, JSON etc.
- The SQLFORM.grid supports following format:
I’ll explain some useful hacks in this export functionality. Create a app called sample_app using appadmin or PyDAL’s shell. Let’s create two tables in database first.
-
You’ll have to add some entries for company and employee tables using appadmin or PyDAL shell.
-
You can use the add functionality of the grid to add some entries. Let’s call the SQLFORM.grid in controllers/index.py
- We’ll have to modify the code in views/default/index.html
- Now check the index page.
Screenshot of Index page.
- It shows various export type like CSV, JSON, HTML etc. But what if user wants to see only CSV format.
Export Classes
- All formats are displayed by default But we can decide whether to display all formats or single format like CSV.
- The exportclasses parameter decides which format of export will display or not.
- modify your code in controllers/default.py
It shows only CSV format and doesn’t show other formats.
- Position of CSV button at the bottm of grid looks odd for user. What if user wants CSV button after search button.
Repositioning the export button
- We can reposition the export buttons for all grids or specific grid.
- If you use following javascript code in views/layout.html then the changes will apply on all grids.
- If you want to apply the changes to employee grid then add the following javascript code in respective view file. In this case, we’ll have to modify views/default/index.html
Save layout.html and refresh the index.html page.
-
Here, Web2Py adds all export links inside w2p_export_menu class. We’re cloning this class and appending cloned class to web2py_console form class.
-
Let’s check whether export to csv works or not. (Click on CSV button)
employee.id | employee.name | employee.email | employee.company |
---|---|---|---|
1 | Tyrion Lannister | dwarf@google.com | 1 |
2 | Jon Snow | jon.snow@apple.com | 3 |
3 | Jaime Lannister | jaime.lannister@microsoft.com | 2 |
4 | Stannis Lanister | stannis.lanister@quora.com | 4 |
5 | Arya Starck | arya.starck@quora.com | 4 |
-
Here,
It shows company id not a company name in employee.company Because company is a foreign key in employee table.
So How can we display company name instead of ID ?
Display name instead of id in export
- We’ll have to write a custom export class for CSV which will write name instead of id.
- Let’s call this class in export_classes. (Modify code in controllers/index.py)
- Click on Export to CSV button and check whether csv contains company names or ids.
employee.id | employee.name | employee.email | employee.company |
---|---|---|---|
1 | Tyrion Lannister | dwarf@google.com | |
2 | Jon Snow | jon.snow@apple.com | Apple |
3 | Jaime Lannister | jaime.lannister@microsoft.com | Microsoft |
4 | Stannis Lanister | stannis.lanister@quora.com | Quora |
5 | Arya Starck | arya.starck@quora.com | Quora |
- Now It shows company names Becasue of the custom CSVEXporter class.
- The exported CSV file’s headers show employee.id, employee.name etc. The headers format looks unmatched.
- It would be better if the headers are displayed as attribute name like ID, NAME, EMAIL etc.
Changing header names
- Actually It gives you headers as “table_name.attribute_name”.
- For e.g: employee.id
- The gluon.dal.Rows’s colnames gives you the current table’s columns names in a list. So we can obtain the all columns of current name.
- Let’s check it in Web2Py’s shell.
- It returns a list which contains column names. It follows “table_name.attribute_name” format.
- We’ll have to remove the “table_name.” So we get only attribute name.
- Let’s use this logic in the custom CSVExporter class.
- Let’s click on Export to CSV button and check whether it gives only attribute names or table_name.attribute_name
ID | NAME | COMPANY | |
---|---|---|---|
1 | Tyrion Lannister | dwarf@google.com | |
2 | Jon Snow | jon.snow@apple.com | Apple |
3 | Jaime Lannister | jaime.lannister@microsoft.com | Microsoft |
4 | Stannis Lanister | stannis.lanister@quora.com | Quora |
5 | Arya Starck | arya.starck@quora.com | Quora |
- It shows only attribute names in headers. We can change header name using this trick.
Skipping columns while exporting.
- We can skip columns while exporting the data.
- For e.g: we can show COMPANY column in grid But we can skip it while exporting it.
- Web2Py includes _export_type in request.vars When you click on any export button
- We’ll have to the fields parameter of SQLFORM.grid which decides whether to skip columns or not.
- Le’t modify code in controllers/default.py
The SQLFORM.grid shows all column names.
- Let’s Click on Export to CSV and check whether company column is skipped or not.
ID | NAME | |
---|---|---|
1 | Tyrion Lannister | dwarf@google.com |
2 | Jon Snow | jon.snow@apple.com |
3 | Jaime Lannister | jaime.lannister@microsoft.com |
4 | Stannis Lanister | stannis.lanister@quora.com |
5 | Arya Starck | arya.starck@quora.com |
- It skipped company column in exported csv file. So we can skip any column using the fields parameter.
Summery
- In this blog post, We’ve learned following things:
- How to use the export functionality.
- Show specified format using exportclasses
- Repositioning the export button.
- Display name instead of ids.
- Changing the header names.
- Skipping columns while exporting the data.
I’ve used web2py’s 2.9 version in above post. The sample app is available here. Report bug if you find any.