{"guide": {"name": "connecting-to-a-database", "category": "data-science-and-plots", "pretty_category": "Data Science And Plots", "guide_index": 4, "absolute_index": 33, "pretty_name": "Connecting To A Database", "content": "# Connecting to a Database\n\nThe data you wish to visualize may be stored in a database. Let's use SQLAlchemy to quickly extract database content into pandas Dataframe format so we can use it in gradio.\n\nFirst install `pip install sqlalchemy` and then let's see some examples.\n\n## SQLite\n\n```python\nfrom sqlalchemy import create_engine\nimport pandas as pd\n\nengine = create_engine('sqlite:///your_database.db')\n\nwith gr.Blocks() as demo:\n gr.LinePlot(pd.read_sql_query(\"SELECT time, price from flight_info;\", engine), x=\"time\", y=\"price\")\n```\n\nLet's see a a more interactive plot involving filters that modify your SQL query:\n\n```python\nfrom sqlalchemy import create_engine\nimport pandas as pd\n\nengine = create_engine('sqlite:///your_database.db')\n\nwith gr.Blocks() as demo:\n origin = gr.Dropdown([\"DFW\", \"DAL\", \"HOU\"], value=\"DFW\", label=\"Origin\")\n\n gr.LinePlot(lambda origin: pd.read_sql_query(f\"SELECT time, price from flight_info WHERE origin = {origin};\", engine), inputs=origin, x=\"time\", y=\"price\")\n```\n\n## Postgres, mySQL, and other databases\n\nIf you're using a different database format, all you have to do is swap out the engine, e.g.\n\n```python\nengine = create_engine('postgresql://username:password@host:port/database_name')\n```\n\n```python\nengine = create_engine('mysql://username:password@host:port/database_name')\n```\n\n```python\nengine = create_engine('oracle://username:password@host:port/database_name')\n```", "tags": [], "spaces": [], "url": "/guides/connecting-to-a-database/", "contributor": null}}