{"id":20876365,"url":"https://github.com/pjcunningham/flask-admin-dashboard-summary","last_synced_at":"2025-05-12T15:32:17.233Z","repository":{"id":53622537,"uuid":"155554487","full_name":"pjcunningham/Flask-Admin-Dashboard-Summary","owner":"pjcunningham","description":"Flask-Admin-Dashboard with summary rows","archived":false,"fork":false,"pushed_at":"2023-05-01T20:56:58.000Z","size":5577,"stargazers_count":32,"open_issues_count":3,"forks_count":11,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-05-10T20:42:18.020Z","etag":null,"topics":["admin-dashboard","flask","flask-admin","python","sqlalchemy"],"latest_commit_sha":null,"homepage":null,"language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/pjcunningham.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2018-10-31T12:34:05.000Z","updated_at":"2024-07-20T18:12:22.000Z","dependencies_parsed_at":"2024-11-18T06:51:36.465Z","dependency_job_id":"9cb09e93-3bac-4765-87a2-85166a8b9b80","html_url":"https://github.com/pjcunningham/Flask-Admin-Dashboard-Summary","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pjcunningham%2FFlask-Admin-Dashboard-Summary","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pjcunningham%2FFlask-Admin-Dashboard-Summary/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pjcunningham%2FFlask-Admin-Dashboard-Summary/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pjcunningham%2FFlask-Admin-Dashboard-Summary/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/pjcunningham","download_url":"https://codeload.github.com/pjcunningham/Flask-Admin-Dashboard-Summary/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253765985,"owners_count":21960826,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["admin-dashboard","flask","flask-admin","python","sqlalchemy"],"created_at":"2024-11-18T06:51:06.480Z","updated_at":"2025-05-12T15:32:16.351Z","avatar_url":"https://github.com/pjcunningham.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"## Flask-Admin Dashboard With Summaries\n\n![Summary](screenshots/summary.jpg)\n\nThis project was created in response to this SO question - [Flask Admin Models - Summary row](https://stackoverflow.com/questions/53017724/flask-admin-models-summary-row). The question references a previous SO question, [How do you add a summary row for Flask-Admin?](https://stackoverflow.com/questions/47124728/how-do-you-add-a-summary-row-for-flask-admin).\n\nThis project is a clone of [Flask-Admin-Dashboard](https://github.com/jonalxh/Flask-Admin-Dashboard) with an extra view (Projects) showing how to add summary rows to a [Flask-Admin](https://github.com/flask-admin/flask-admin) list view.\n\nThis project differentiates from the original:\n\n- Uses the new Flask methods for running the project\n- The database is initialized via a flask command ```create-database``` defined in commands.py\n- Database and models have been placed in models.py\n- Flask-Admin views have been placed in views.py\n\n### Initializing the database\n\nThe project comes with a pre-initialized database (```sample_db.sqlite```). To create a new database, from a CLI, in the root of the project run:\n\n    \u003e flask create-database\n\n### Running Flask\n\nFrom the CLI, in the root of the project run:\n\n    \u003e flask run\n\n     * Serving Flask app \"app/__init__.py\"\n     * Environment: production\n       WARNING: Do not use the development server in a production environment.\n       Use a production WSGI server instead.\n     * Debug mode: off\n     * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)\n\n\n###\n\nTo show a summary table a view needs to:\n\n- inject summary values into the view\n- define what Jinja template to use and suitably modify it to use the injected summary values\n\n#### Setting up the Jinja template\n\n```templates/admin/model/summary_list.html``` is a direct copy of [list.html](https://github.com/flask-admin/flask-admin/blob/master/flask_admin/templates/bootstrap3/admin/model/list.html)\n\nNote the file name, ```summary_list.html```, as this is used in the view definition's ```render``` method.\n\nThe following block of html has been inserted at line 163:\n\n\t{# This adds the summary data #}\n\t{% for row in summary_data %}\n\t\u003ctr\u003e\n\t    {% if actions %}\n\t    \u003ctd\u003e\n\t        {# leave this empty #}\n\t    \u003c/td\u003e\n\t    {% endif %}\n\t    {# This is the summary line title and goes in the action column, note that the action may not be visible!!! #}\n\t    {% if admin_view.column_display_actions %}\n\t        \u003ctd\u003e\u003cstrong\u003e{{ row['title'] or ''}}\u003c/strong\u003e\u003c/td\u003e\n\t    {% endif %}\n\t    {# This is the summary line data and goes in the individual columns #}\n\t    {% for c, name in list_columns %}\n\t        \u003ctd class=\"col-{{c}}\"\u003e\n\t            \u003cstrong\u003e{{ row[c] or ''}}\u003c/strong\u003e\n\t        \u003c/td\u003e\n\t    {% endfor %}\n\t\u003c/tr\u003e\n\t{% endfor %}\n\n\n#### Setting up the view\n\n```views.py``` beginning at line 60.\n\nLine 61, define the template to use:\n\n    # don't call the custom page list.html as you'll get a recursive call\n    list_template = 'admin/model/summary_list.html'\n\nLine 75, override the view's ```render(self, template, **kwargs)``` method:\n\n    def render(self, template, **kwargs):\n        # we are only interested in the summary_list page\n        if template == 'admin/model/summary_list.html':\n            # append a summary_data dictionary into kwargs\n            # The title attribute value appears in the actions column\n            # all other attributes correspond to their respective Flask-Admin 'column_list' definition\n            _current_page = kwargs['page']\n            kwargs['summary_data'] = [\n                {'title': 'Page Total', 'name': None, 'cost': self.page_cost(_current_page)},\n                {'title': 'Grand Total', 'name': None, 'cost': self.total_cost()},\n            ]\n        return super(ProjectView, self).render(template, **kwargs)\n\nNote the conditional check on the template as we're not concerned with the Edit/Create rendering and the injection of a ```summary_data``` dictionary into the method's ```**kwargs``` argument.\n\nNote the helper methods to provide the actual summary data at lines 66 and 71, these need to be adjusted as necessary:\n\n    def page_cost(self, current_page):\n        # this should take into account any filters/search inplace\n        _query = self.session.query(Project).limit(self.page_size).offset(current_page * self.page_size)\n        return sum([p.cost for p in _query])\n    \n    def total_cost(self):\n        # this should take into account any filters/search inplace\n        return self.session.query(func.sum(Project.cost)).scalar()\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpjcunningham%2Fflask-admin-dashboard-summary","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpjcunningham%2Fflask-admin-dashboard-summary","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpjcunningham%2Fflask-admin-dashboard-summary/lists"}