{"id":25924867,"url":"https://github.com/tinaland101/sqlalchemy-challenge","last_synced_at":"2025-03-03T18:13:54.556Z","repository":{"id":279848668,"uuid":"908657552","full_name":"tinaland101/sqlalchemy-challenge","owner":"tinaland101","description":"The purpose of this project is to analyze and explore climate data for Honolulu, Hawaii, to assist with trip planning by assessing weather patterns. It involves using SQLAlchemy to interact with a SQLite database that contains historical weather data. The analysis focuses on precipitation and temperature data.","archived":false,"fork":false,"pushed_at":"2025-02-27T20:31:28.000Z","size":420,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-02-28T03:48:32.929Z","etag":null,"topics":["flask-api","flask-sqlalchemy","mathplotlib","numpy-library"],"latest_commit_sha":null,"homepage":"","language":"Jupyter Notebook","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/tinaland101.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":"2024-12-26T16:07:42.000Z","updated_at":"2025-02-27T20:31:31.000Z","dependencies_parsed_at":"2025-02-28T03:52:12.174Z","dependency_job_id":"ab8d6b2f-8d75-4de0-bb29-6a3c33a3c533","html_url":"https://github.com/tinaland101/sqlalchemy-challenge","commit_stats":null,"previous_names":["tinaland101/sqlalchemy-challenge"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tinaland101%2Fsqlalchemy-challenge","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tinaland101%2Fsqlalchemy-challenge/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tinaland101%2Fsqlalchemy-challenge/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tinaland101%2Fsqlalchemy-challenge/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/tinaland101","download_url":"https://codeload.github.com/tinaland101/sqlalchemy-challenge/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":241715018,"owners_count":20007914,"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":["flask-api","flask-sqlalchemy","mathplotlib","numpy-library"],"created_at":"2025-03-03T18:13:53.926Z","updated_at":"2025-03-03T18:13:54.544Z","avatar_url":"https://github.com/tinaland101.png","language":"Jupyter Notebook","readme":"# sqlalchemy-challenge\n\nDatabase Schema\nThe SQLite database (hawaii.sqlite) contains two tables:\n\nMeasurement Table\n\nid: Primary Key\nstation: Station ID\ndate: Observation date\nprcp: Precipitation amount\ntobs: Temperature observation\nStation Table\n\nid: Primary Key\nstation: Unique station ID\nname: Station name\nlatitude: Latitude coordinate\nlongitude: Longitude coordinate\nelevation: Elevation\nData Analysis Steps\n1. Database Connection\nThe project uses SQLAlchemy ORM to connect to the hawaii.sqlite database:\n\n\nfrom sqlalchemy import create_engine\nfrom sqlalchemy.ext.automap import automap_base\nfrom sqlalchemy.orm import Session\n\n# Create engine and reflect tables\nengine = create_engine(\"sqlite:///Resources/hawaii.sqlite\")\nBase = automap_base()\nBase.prepare(engine, reflect=True)\n\n# Save references to tables\nStation = Base.classes.station\nMeasurement = Base.classes.measurement\n\n# Create session\nsession = Session(engine)\n2. Precipitation Analysis\nRetrieve the last 12 months of precipitation data from the Measurement table.\nConvert results into a Pandas DataFrame.\nSort the data by date.\nVisualize precipitation trends using Matplotlib.\n\nimport pandas as pd\nimport datetime as dt\nimport matplotlib.pyplot as plt\n\n# Find the most recent date\nmost_recent_date = session.query(Measurement.date).order_by(Measurement.date.desc()).first()[0]\n\n# Calculate the date one year ago\none_year_ago = dt.datetime.strptime(most_recent_date, \"%Y-%m-%d\") - dt.timedelta(days=365)\n\n# Query precipitation data for the last 12 months\nprecipitation_data = session.query(Measurement.date, Measurement.prcp).\\\n    filter(Measurement.date \u003e= one_year_ago).order_by(Measurement.date).all()\n\n# Convert to Pandas DataFrame\nprecipitation_df = pd.DataFrame(precipitation_data, columns=['date', 'precipitation'])\nprecipitation_df = precipitation_df.dropna()  # Remove NaN values\n\n# Plot precipitation data\nplt.figure(figsize=(10,6))\nplt.bar(precipitation_df['date'], precipitation_df['precipitation'], width=3, color='b', alpha=0.5)\nplt.xticks(rotation=90)\nplt.xlabel(\"Date\")\nplt.ylabel(\"Inches\")\nplt.title(\"Precipitation in the Last 12 Months\")\nplt.show()\n3. Station Analysis\nFind the total number of stations in the dataset.\nIdentify the most active station (station with the most observations).\nRetrieve min, max, and avg temperatures for the most active station.\nVisualize temperature data using a histogram.\nFind the total number of stations\n\ntotal_stations = session.query(Station).count()\nprint(f\"Total number of stations: {total_stations}\")  # Output: 9\nIdentify the most active station\npython\nCopy\nfrom sqlalchemy import func\n\n# Query the most active stations (station with most observations)\nmost_active_stations = session.query(Measurement.station, func.count(Measurement.station))\\\n    .group_by(Measurement.station)\\\n    .order_by(func.count(Measurement.station).desc())\\\n    .all()\n\nmost_active_station = most_active_stations[0][0]  # Extract the top station ID\nprint(f\"Most active station: {most_active_station}\")  # Output: 'USC00519281'\nRetrieve min, max, and avg temperatures for the most active station\npython\nCopy\ntemperature_stats = session.query(func.min(Measurement.tobs),\n                                  func.max(Measurement.tobs),\n                                  func.avg(Measurement.tobs))\\\n    .filter(Measurement.station == most_active_station).all()\n\nprint(f\"Temperature stats for {most_active_station}: Min={temperature_stats[0][0]}, Max={temperature_stats[0][1]}, Avg={temperature_stats[0][2]}\")\nExpected Output:\n\nmathematica\n\nTemperature stats for USC00519281: Min=54.0, Max=85.0, Avg=71.66\nPlot Temperature Observations\n\n# Query last 12 months of temperature observations for most active station\ntobs_data = session.query(Measurement.tobs).filter(Measurement.station == most_active_station)\\\n    .filter(Measurement.date \u003e= one_year_ago).all()\n\n# Convert to DataFrame\ntobs_df = pd.DataFrame(tobs_data, columns=['tobs'])\n\n# Plot histogram\ntobs_df.plot.hist(bins=12, figsize=(10,6), legend=False)\nplt.title(f\"Temperature Observations for Station {most_active_station}\")\nplt.xlabel(\"Temperature (°F)\")\nplt.ylabel(\"Frequency\")\nplt.show()\nFinal Analysis \u0026 Observations\nThe dataset covers August 23, 2016 – August 23, 2017.\nAverage daily rainfall was 0.18 inches, with some days receiving up to 6.7 inches.\nHonolulu generally experiences low rainfall, but occasional heavy downpours occur.\nThe most active station (USC00519281) recorded 2,772 observations.\nTemperature trends show an average of 72°F, with a low of 54°F and a high of 85°F.\nMost days are warm and pleasant, making Honolulu a great outdoor destination.\nHow to Run the Code\nInstall Dependencies\nEnsure Python is installed and install required libraries:\n\npip install numpy pandas matplotlib sqlalchemy\nRun the Jupyter Notebook\nOpen climate_analysis.ipynb and execute the cells to perform climate analysis.\nClosing the Database Session\nTo ensure the database connection is properly managed, the session is closed at the end of the script:\n\n\n# Close Session\nsession.close()\nNext Steps\nDevelop a Flask API (app.py) to retrieve and serve climate data dynamically.\nExpand analysis by comparing climate trends across multiple stations.\nBuild an interactive visualization dashboard for user-friendly insights.\nContact \u0026 Contributions\nIf you have any suggestions or improvements, feel free to contribute! 🚀\n\nThis README.md explains the project setup, code functionality, and key findings in a clear and structured way. Let me know if you'd like any modifications! 🚀\n\n\n\n\n\n\nDeveloping app.py for this project involves creating a Flask API that serves climate data through various routes. Below is a step-by-step guide to help you build app.py for the Honolulu Climate Analysis API.\n\nStep 1: Install Flask\nBefore proceeding, ensure Flask is installed in your environment:\n\n\npip install flask sqlalchemy pandas numpy\nStep 2: Import Required Libraries\nCreate a new Python script called app.py, then import the necessary dependencies:\n\n\nfrom flask import Flask, jsonify\nimport numpy as np\nimport pandas as pd\nimport datetime as dt\nfrom sqlalchemy import create_engine, func\nfrom sqlalchemy.ext.automap import automap_base\nfrom sqlalchemy.orm import Session\nStep 3: Set Up Database Connection\nConnect Flask to the SQLite database (hawaii.sqlite) using SQLAlchemy:\n\n\n# Create the Flask app\napp = Flask(__name__)\n\n# Create engine and reflect database tables\nengine = create_engine(\"sqlite:///Resources/hawaii.sqlite\")\nBase = automap_base()\nBase.prepare(engine, reflect=True)\n\n# Save references to the tables\nStation = Base.classes.station\nMeasurement = Base.classes.measurement\nStep 4: Define API Routes\nNow, define the API endpoints:\n\n1. Homepage Route (/)\nThis will list all available API routes:\n\n\n@app.route(\"/\")\ndef welcome():\n    return (\n        f\"Welcome to the Honolulu Climate API!\u003cbr/\u003e\"\n        f\"Available Routes:\u003cbr/\u003e\"\n        f\"/api/v1.0/precipitation - Last 12 months of precipitation data\u003cbr/\u003e\"\n        f\"/api/v1.0/stations - List of weather stations\u003cbr/\u003e\"\n        f\"/api/v1.0/tobs - Temperature observations from the most active station\u003cbr/\u003e\"\n        f\"/api/v1.0/\u003cstart\u003e - Temperature stats from a start date to the most recent date\u003cbr/\u003e\"\n        f\"/api/v1.0/\u003cstart\u003e/\u003cend\u003e - Temperature stats for a given date range\u003cbr/\u003e\"\n    )\n2. Precipitation Route (/api/v1.0/precipitation)\nReturns the last 12 months of precipitation data in JSON format.\n\n\n@app.route(\"/api/v1.0/precipitation\")\ndef precipitation():\n    # Start a session\n    session = Session(engine)\n    \n    # Find the most recent date\n    most_recent_date = session.query(Measurement.date).order_by(Measurement.date.desc()).first()[0]\n    \n    # Calculate one year ago from the most recent date\n    one_year_ago = dt.datetime.strptime(most_recent_date, \"%Y-%m-%d\") - dt.timedelta(days=365)\n\n    # Query precipitation data for the last 12 months\n    results = session.query(Measurement.date, Measurement.prcp).\\\n        filter(Measurement.date \u003e= one_year_ago).all()\n\n    session.close()\n\n    # Convert query results to dictionary\n    precipitation_dict = {date: prcp for date, prcp in results}\n    \n    return jsonify(precipitation_dict)\n3. Stations Route (/api/v1.0/stations)\nReturns a JSON list of all weather stations.\n\n\n@app.route(\"/api/v1.0/stations\")\ndef stations():\n    session = Session(engine)\n\n    # Query all station names\n    results = session.query(Station.station).all()\n    \n    session.close()\n\n    # Convert list of tuples into normal list\n    station_list = list(np.ravel(results))\n\n    return jsonify(station_list)\n4. Temperature Observations Route (/api/v1.0/tobs)\nReturns the last 12 months of temperature observations for the most active station.\n\npython\nCopy\n@app.route(\"/api/v1.0/tobs\")\ndef tobs():\n    session = Session(engine)\n\n    # Find the most active station\n    most_active_station = session.query(Measurement.station).\\\n        group_by(Measurement.station).\\\n        order_by(func.count(Measurement.station).desc()).first()[0]\n\n    # Find the most recent date\n    most_recent_date = session.query(Measurement.date).order_by(Measurement.date.desc()).first()[0]\n\n    # Calculate one year ago from the most recent date\n    one_year_ago = dt.datetime.strptime(most_recent_date, \"%Y-%m-%d\") - dt.timedelta(days=365)\n\n    # Query temperature observations for last 12 months for the most active station\n    results = session.query(Measurement.date, Measurement.tobs).\\\n        filter(Measurement.station == most_active_station).\\\n        filter(Measurement.date \u003e= one_year_ago).all()\n\n    session.close()\n\n    # Convert to a list\n    tobs_list = list(np.ravel(results))\n\n    return jsonify(tobs_list)\n5. Temperature Stats Route (Start Date: /api/v1.0/\u003cstart\u003e)\nReturns min, max, and avg temperature from a given start date to the most recent date.\n\n\n@app.route(\"/api/v1.0/\u003cstart\u003e\")\ndef temp_stats_start(start):\n    session = Session(engine)\n\n    # Query min, max, and avg temperature from the given start date\n    results = session.query(func.min(Measurement.tobs), \n                            func.avg(Measurement.tobs), \n                            func.max(Measurement.tobs)).\\\n        filter(Measurement.date \u003e= start).all()\n\n    session.close()\n\n    # Convert query result into a dictionary\n    temp_stats = {\n        \"Min Temperature\": results[0][0],\n        \"Average Temperature\": results[0][1],\n        \"Max Temperature\": results[0][2]\n    }\n\n    return jsonify(temp_stats)\n6. Temperature Stats Route (Start \u0026 End Date: /api/v1.0/\u003cstart\u003e/\u003cend\u003e)\nReturns min, max, and avg temperature for a given date range.\n\n\n@app.route(\"/api/v1.0/\u003cstart\u003e/\u003cend\u003e\")\ndef temp_stats_start_end(start, end):\n    session = Session(engine)\n\n    # Query min, max, and avg temperature between start and end dates\n    results = session.query(func.min(Measurement.tobs), \n                            func.avg(Measurement.tobs), \n                            func.max(Measurement.tobs)).\\\n        filter(Measurement.date \u003e= start).\\\n        filter(Measurement.date \u003c= end).all()\n\n    session.close()\n\n    # Convert query result into a dictionary\n    temp_stats = {\n        \"Min Temperature\": results[0][0],\n        \"Average Temperature\": results[0][1],\n        \"Max Temperature\": results[0][2]\n    }\n\n    return jsonify(temp_stats)\nStep 5: Run the Flask App\nAt the bottom of app.py, add:\n\n\nif __name__ == \"__main__\":\n    app.run(debug=True)\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftinaland101%2Fsqlalchemy-challenge","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftinaland101%2Fsqlalchemy-challenge","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftinaland101%2Fsqlalchemy-challenge/lists"}