项目作者: ArchitaDesai

项目描述 :
高级语言: JavaScript
项目地址: git://github.com/ArchitaDesai/Weather-App-React-Django.git
创建时间: 2019-04-20T08:08:36Z
项目社区:https://github.com/ArchitaDesai/Weather-App-React-Django

开源协议:

下载


WeatherFinder

A React and Django web-app that display weather based on the city entered by the user.

Features

Start typing a city name and autosuggestions will appear
Autosuggestions in Desktop

AutoSuggestions Desktop View

Weather information

Desktop View of Weather data

Respoonsive React App - works well in tablet and mobile views

AutoSuggestions in Mobile View
Mobile view of weather data

Installation Instructions

  • After cloning the repository, cd into it and run following commands:
  1. # Create a virtual environment for the backend
  2. virtualenv env
  3. # Activate the virtual environment
  4. source env/bin/activate
  5. # Install the packages
  6. pip install -r requirements.txt
  7. # Make build.sh executable
  8. chmod +x build.sh
  9. # Install nodemon gloablly to auto-reload the server when making changes in the files
  10. npm install -g nodemon
  11. # cd into the frontend directory
  12. cd frontend
  13. # Install packages for the React app
  14. npm install
  15. # Use npm-watch to track the changes being made in the React app
  16. npm run watch
  1. source env/bin/activate
  2. cd backend
  3. # To run Django dev server and watch file changes in the app
  4. nodemon --exec python manage.py runserver

Technical Details

Code Architecture

Overview

  • frontend directory:
    • React app, which can be run separately on its own by running npm start in the frontend directory, after installing the node_modules.
  • backend directory:
    • Main Django app, on which the whole application is running.
  • build.sh:
    • For automating build scripts of React and using those static files as Django templates
  • env:
    • Virtual environment for the Django project
  • requirements.txt:
    • Dependency list of the Django app

Workflow

  • Inside frontend/package.json, npm-watch module is included as a dev dependency because it will detect changes in the React app (frontend/src and frontend/public) and buildapp will run build.sh script.
  • Every time build.sh script runs, it will generate static bundle files from the React app and ./manage.py collectstatic will collect those static files and store them inside backend/static
  • Whitenoise:
    • This package is used to serve and cache static files from frontend/build/root/ whenever needed.
  • Files stored at frontend/build/static will be served at /static/ URL, settings for the same are specified in backend/weatherfinder/settings.py.
  • Django’s urls.py will go to views.py, which will render index.html file of React app.
  • This index.html when rendered, will have links to static JS bundle files and they’ll be considered as Django templates and served from there.
  • Nodemon:
    • Keeps track of changes being made in the file and reruns the file or server based on changes in the source code.
  • nodemon --exec python manage.py runserver will run the development server and every time some changes happen in the Django web-app or its static files, nodemon will re-start the django dev server.

APIs Used:

Google Places Autocomplete API

OpenWeatherMap API

  • To display weather based on city, country, latitude, longitude, etc.
OpenWeatherMap API Endpoints
Weather display data JSON details
  • Type of weather
    • weather[0].description
  • Weather type icon
    • weather[0].icon
  • latitude, longitude
    • coord.lon, coord.lat
  • temperature (given in K) = (K-273) celcius
    • main.temp
  • Humidity in %
    • main.humidity
  • Wind speed (given in m/s) = ms*3.6 kmph
    • wind.speed

Reference: Hybrid app model used to integrate React app with Django