{"id":16174909,"url":"https://github.com/app-generator/sample-flask-pandas-dataframe","last_synced_at":"2025-08-15T15:18:12.234Z","repository":{"id":52989008,"uuid":"356555315","full_name":"app-generator/sample-flask-pandas-dataframe","owner":"app-generator","description":"Flask Pandas Dataframe - Open-source sample | AppSeed","archived":false,"fork":false,"pushed_at":"2024-03-08T15:08:24.000Z","size":93,"stargazers_count":17,"open_issues_count":1,"forks_count":21,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-08-12T19:49:10.512Z","etag":null,"topics":["appseed-sample","flask-sample","pandas"],"latest_commit_sha":null,"homepage":"https://blog.appseed.us/flask-pandas-dataframe-how-to-code/","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/app-generator.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE.md","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":"2021-04-10T11:04:20.000Z","updated_at":"2025-04-06T02:37:56.000Z","dependencies_parsed_at":"2024-10-27T19:21:13.461Z","dependency_job_id":null,"html_url":"https://github.com/app-generator/sample-flask-pandas-dataframe","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"purl":"pkg:github/app-generator/sample-flask-pandas-dataframe","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/app-generator%2Fsample-flask-pandas-dataframe","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/app-generator%2Fsample-flask-pandas-dataframe/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/app-generator%2Fsample-flask-pandas-dataframe/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/app-generator%2Fsample-flask-pandas-dataframe/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/app-generator","download_url":"https://codeload.github.com/app-generator/sample-flask-pandas-dataframe/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/app-generator%2Fsample-flask-pandas-dataframe/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":270586481,"owners_count":24611317,"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","status":"online","status_checked_at":"2025-08-15T02:00:12.559Z","response_time":110,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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":["appseed-sample","flask-sample","pandas"],"created_at":"2024-10-10T04:43:44.662Z","updated_at":"2025-08-15T15:18:12.172Z","avatar_url":"https://github.com/app-generator.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Flask Pandas Dataframe\n\nSimple Flask project that loads pandas dataframe into the database and shows the information on a page. **[Flask Pandas Dataframe](https://blog.appseed.us/flask-pandas-dataframe-how-to-code/)** is a `one-file` project that might help beginners to understand some basic Flask concepts:  \n\n- Create a simple Flask [app](/app.py)\n- Download a public [pandas](./titanic.csv) DF\n- Create an SQLite DB and a table to save the information\n- Load pandas in DB using a new `custom command`\n- Visualize the data in the browser\n\n\u003cbr /\u003e\n\n\u003e Links \n\n- 👉 [Support](https://appseed.us/support/) - Provided by [AppSeed](https://appseed.us/)\n- 👉 More [Open-Source Starters](https://appseed.us/admin-dashboards/open-source/) - actively supported and versioned \n\n\u003cbr /\u003e\n\n**Pandas Dataframe** - Console View\n\n![Flask Pandas Dataframe - media file.](https://raw.githubusercontent.com/app-generator/flask-pandas-dataframe/main/media/screen.png)\n\n\u003cbr /\u003e\n\n**Pandas Dataframe** - Browser View\n\n![Flask Pandas Dataframe - visualize data in the browser.](https://raw.githubusercontent.com/app-generator/flask-pandas-dataframe/main/media/loaded-data.png)\n\n\u003cbr /\u003e\n\n## Set up\n\n```bash\n$ # Clone sources\n$ git clone https://github.com/app-generator/flask-pandas-dataframe.git\n$ cd flask-pandas-dataframe\n$\n$ # Virtualenv modules installation (Unix based systems)\n$ virtualenv env\n$ source env/bin/activate\n$\n$ # Virtualenv modules installation (Windows based systems)\n$ # virtualenv env\n$ # .\\env\\Scripts\\activate\n$\n$ # Install dependencies\n$ pip3 install -r requirements.txt\n$\n$ # Create database via Flask CLI\n$ flask shell\n\u003e\u003e\u003e from app import db  # import SqlAlchemy interface \n\u003e\u003e\u003e db.create_all()     # create SQLite database and Data table \n\u003e\u003e\u003e quit()              # leave the Flask CLI  \n$\n$ # Load the data into the database\n$ flask load-data titanic-min.csv\n$\n$ # Set the FLASK_APP environment variable\n$ (Unix/Mac) export FLASK_APP=run.py\n$ (Windows) set FLASK_APP=run.py\n$ (Powershell) $env:FLASK_APP = \".\\run.py\"\n$\n$ # Set up the DEBUG environment\n$ # (Unix/Mac) export FLASK_ENV=development\n$ # (Windows) set FLASK_ENV=development\n$ # (Powershell) $env:FLASK_ENV = \"development\"\n$ \n$ flask run \n$ # access the app in the browser: http://localhost:5000\n```\n\n\u003cbr /\u003e\n\n## Dependencies\n\n- [Flask](https://flask.palletsprojects.com/en/1.1.x/) - the framework used  \n- [Pandas](https://pandas.pydata.org/) - an amazing `data analysis` library\n- [SQLAlchemy](https://www.sqlalchemy.org/) - Python SQL Toolkit and ORM\n- [Flask-SqlAlchemy](https://flask-sqlalchemy.palletsprojects.com/en/2.x/) - extension for Flask that adds support for SQLAlchemy\n- [Requests](https://pypi.org/project/requests/) - simple HTTP library. \n\n```bash\n$ # Virtualenv modules installation (Unix based systems)\n$ virtualenv env\n$ source env/bin/activate\n$\n$ # Virtualenv modules installation (Windows based systems)\n$ # virtualenv env\n$ # .\\env\\Scripts\\activate\n$\n$ # Install modules - SQLite Database\n$ pip3 install -r requirements.txt\n```\n\u003cbr /\u003e\n\n## Env\n\n```bash\n$ # Enable the DEBUG environment\n$ # (Unix/Mac) export FLASK_ENV=development\n$ # (Windows) set FLASK_ENV=development\n$ # (Powershell) $env:FLASK_ENV = \"development\"\n$\n$ # Set the FLASK_APP environment variable\n$ (Unix/Mac) export FLASK_APP=app.py\n$ (Windows) set FLASK_APP=app.py\n$ (Powershell) $env:FLASK_APP = \".\\app.py\"\n```\n\n\u003cbr/\u003e\n\n## Download the data\n\nThe dataset is downloaded from a remote [location](https://static.appseed.us/data/titanic.txt) and saved [locally](./titanic.csv).\n\n```python\n\u003e\u003e\u003e import requests\n\u003e\u003e\u003e import pandas as pd\n\u003e\u003e\u003e \n\u003e\u003e\u003e # Define the remote CSV file\n\u003e\u003e\u003e csv_file = 'https://static.appseed.us/data/titanic.txt'\n\u003e\u003e\u003e\n\u003e\u003e\u003e # Download the file (via request library)\n\u003e\u003e\u003e r = requests.get( csv_file )\n\u003e\u003e\u003e \n\u003e\u003e\u003e # Save the content to a new LOCAL file\n\u003e\u003e\u003e f = open('titanic.csv', 'w')  \n\u003e\u003e\u003e f.write( r.content.decode(\"utf-8\") )\n\u003e\u003e\u003e f.close   \n```\n\n\u003cbr /\u003e\n\n## Process data\n\nThe RAW dataset has ~900 rows and we can inspect it with ease using `pandas` library \n\n```python\n\u003e\u003e\u003e import pandas as pd\n\u003e\u003e\u003e \n\u003e\u003e\u003e df = pd.read_csv( 'titanic.csv' )\n\u003e\u003e\u003e df\n     PassengerId  Survived  Pclass                                               Name     Sex   Age  SibSp  Parch            Ticket     Fare Cabin Embarked\n0              1         0       3                            Braund, Mr. Owen Harris    male  22.0      1      0         A/5 21171   7.2500   NaN        S\n1              2         1       1  Cumings, Mrs. John Bradley (Florence Briggs Th...  female  38.0      1      0          PC 17599  71.2833   C85        C\n2              3         1       3                             Heikkinen, Miss. Laina  female  26.0      0      0  STON/O2. 3101282   7.9250   NaN        S\n3              4         1       1       Futrelle, Mrs. Jacques Heath (Lily May Peel)  female  35.0      1      0            113803  53.1000  C123        S\n4              5         0       3                           Allen, Mr. William Henry    male  35.0      0      0            373450   8.0500   NaN        S\n..           ...       ...     ...                                                ...     ...   ...    ...    ...               ...      ...   ...      ...\n886          887         0       2                              Montvila, Rev. Juozas    male  27.0      0      0            211536  13.0000   NaN        S\n887          888         1       1                       Graham, Miss. Margaret Edith  female  19.0      0      0            112053  30.0000   B42        S\n888          889         0       3           Johnston, Miss. Catherine Helen \"Carrie\"  female   NaN      1      2        W./C. 6607  23.4500   NaN        S\n889          890         1       1                              Behr, Mr. Karl Howell    male  26.0      0      0            111369  30.0000  C148        C\n890          891         0       3                                Dooley, Mr. Patrick    male  32.0      0      0            370376   7.7500   NaN        Q\n```\n\n\u003cbr /\u003e\n\nReturn columns data types in the DataFrame: `df.dtypes`. This informationis used to design a `table` where is information is loaded.\n\n```python\n\u003e\u003e\u003e df.dtypes \nPassengerId      int64\nSurvived         int64\nPclass           int64\nName            object\nSex             object\nAge            float64\nSibSp            int64\nParch            int64\nTicket          object\nFare           float64\nCabin           object\nEmbarked        object\n```\n\n\u003cbr /\u003e\n\n## Prepare the `storage`\n\nIntegrate SQLAlchemy and define a table to load the data. \n\n```python\n# Store the Titanic sad stats\nclass Data(db.Model):\n\n    passengerId  = db.Column(db.Integer,     primary_key=True )\n    name         = db.Column(db.String(250), nullable=False   )\n    survived     = db.Column(db.Integer,     nullable=False   )\n    sex          = db.Column(db.String(10 ), default=None     ) # name, female\n    age          = db.Column(db.Integer,     default=-1       ) \n    fare         = db.Column(db.Float,       default=-1       )\n\n    # The string representation\n    def __repr__(self):\n        return str(self.passengerId) + ' - ' + str(self.name) \n```\n\n\u003cbr /\u003e\n\nCreate the SQLite database and the new table via `Flask CLI`: \n\n```bash\n$ flask shell\nApp: app [development]\nInstance: D:\\work\\repo-learn\\python\\how-to\\instance\n\u003e\u003e\u003e from app import db\n\u003e\u003e\u003e db.create_all()\n```\n\nAt this point, we can inspect the database using [SQLiteBrowser](https://sqlitebrowser.org/), an open-source and free editor for SQLite (the table is empty).\n\n\u003cbr /\u003e\n\n## Load Data\n\nThe information will be loaded into the database via a `custom command` = **load-data**. The command expects the `input file` as argument (CSV format). \n\n```python\n# New import\nimport click\n\n...\n\n# Custom command\n@app.cli.command(\"load-data\")\n@click.argument(\"fname\")\ndef load_data(fname):\n    ''' Load data from a CSV file '''\n    print ('*** Load from file: ' + fname)\n\n    # The functional part goes here\n    ... \n```\n\nTo check the command is properly coded we can type `flask --help` in the terminal:\n\n```bash\n$ flask --help\n\nOptions:\n  --version  Show the flask version\n  --help     Show this message and exit.\n\nCommands:\n  load-data  Load data from a CSV file          \u003c-- NEW Command\n  routes     Show the routes for the app.\n  run        Run a development server.\n  shell      Run a shell in the app context.\n```\n\n\u003cbr /\u003e\n\n## Links \u0026 Resources\n\n- [Flask](https://flask.palletsprojects.com/en/1.1.x/) - the framework used  \n- [Pandas](https://pandas.pydata.org/) - an amazing `data analysis` library\n- [AppSeed](https://appseed.us/) - for support annd more samples\n\n\u003cbr /\u003e\n\n---\nFlask Pandas Dataframe - Open-source sample provided by **AppSeed [App Generator](https://appseed.us/app-generator)**. \n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fapp-generator%2Fsample-flask-pandas-dataframe","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fapp-generator%2Fsample-flask-pandas-dataframe","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fapp-generator%2Fsample-flask-pandas-dataframe/lists"}