Skip to content

Commit

Permalink
refactor: refactored into package, moved config.py, removed unused co…
Browse files Browse the repository at this point in the history
…mponents
  • Loading branch information
leehanchung authored and hurshd0 committed Apr 17, 2020
1 parent f36a31a commit e0b808e
Show file tree
Hide file tree
Showing 27 changed files with 249 additions and 288 deletions.
1 change: 1 addition & 0 deletions VERSION
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
2.1.0
29 changes: 24 additions & 5 deletions ncov19_dash/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,26 @@
from .dash_app import app
from .flask_server import server
from .cache import server_cache
###############################################################################
# 2020-04-15: Han:
#
# To run the app using Flask directly, please set environment variable with
# $Env:FLASK_APP='ncov19_dash:server' for windows, or
# export FLASK_APP=ncov19_dash:server for Linux.
#
# And `flask run` to run the app. Alternatively, do `python run.py` at the
# project root directory for dev mode.
#
# Set default config to ProductionConfig unless STAGING environment
# is set to true on Linux `export STAGING=True` or Windows Powershell
# `$Env:STAGING="True"`. Using os.environ directly will throw errors
# if not set.
#
# Changing the order of the imports might cause circular import problem.
# Flask server is created before dash app to allow proper flask routing.
###############################################################################
from ncov19_dash.config import PACKAGE_ROOT, config
from ncov19_dash.cache import server_cache
from ncov19_dash.flask_server import server
from ncov19_dash.dash_app import app

# from ncov19_dash.app import create_app

# app = create_app()
with open(PACKAGE_ROOT / "VERSION") as version_file:
__version__ = version_file.read().strip()
8 changes: 8 additions & 0 deletions ncov19_dash/cache.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
###############################################################################
#
# 2020-04-15 Han: Caching has to be abstracted out to avoid circular imports.
# When caching is implemented inside __init__.py, flask_server.py, or
# dash_app.py, the components will have to import the app/server, creating
# circular import problems.
#
################################################################################
from flask_caching import Cache


Expand Down
9 changes: 4 additions & 5 deletions ncov19_dash/components/__init__.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
# from .confirmed_cases_chart import confirmed_cases_chart
from .daily_stats import daily_stats
from .new_infection_trajectory_chart import new_infection_trajectory_chart
from .infection_trajectory_chart import infection_trajectory_chart
from .news_feed import news_feed
from .scatter_mapbox import confirmed_scatter_mapbox, drive_thru_scatter_mapbox
from .scatter_mapbox import confirmed_scatter_mapbox
from .scatter_mapbox import drive_thru_scatter_mapbox
from .twitter_feed import twitter_feed
from .daily_stats_mobile import daily_stats_mobile
from .column_stats import last_updated
from .column_stats_mobile import mobile_last_updated
from .last_updated import last_updated
from .deaths_chart import deaths_chart
from .cases_chart import cases_chart
from .stats_table import stats_table
2 changes: 1 addition & 1 deletion ncov19_dash/components/cases_chart.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import plotly.graph_objects as go
from ncov19_dash.cache import server_cache
from ncov19_dash.utils import REVERSE_STATES_MAP
from ncov19_dash.utils import config
from ncov19_dash import config


def human_format(num):
Expand Down
27 changes: 0 additions & 27 deletions ncov19_dash/components/column_stats_mobile.py

This file was deleted.

2 changes: 1 addition & 1 deletion ncov19_dash/components/daily_stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import dash_bootstrap_components as dbc
import dash_html_components as html

from ncov19_dash.utils import config
from ncov19_dash import config


def safe_div(x, y):
Expand Down
2 changes: 1 addition & 1 deletion ncov19_dash/components/daily_stats_mobile.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import dash_html_components as html

from ncov19_dash.cache import server_cache
from ncov19_dash.utils import config
from ncov19_dash import config
from ncov19_dash.utils import STATES_COORD


Expand Down
2 changes: 1 addition & 1 deletion ncov19_dash/components/deaths_chart.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import plotly.graph_objects as go
from ncov19_dash.cache import server_cache
from ncov19_dash.utils import REVERSE_STATES_MAP
from ncov19_dash.utils import config
from ncov19_dash import config


def human_format(num):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,17 @@
import plotly.graph_objects as go
from ncov19_dash.cache import server_cache
from ncov19_dash.utils import REVERSE_STATES_MAP
from ncov19_dash.utils import config
from ncov19_dash import config


@server_cache.memoize(timeout=3600)
def new_infection_trajectory_chart(state="US") -> go.Figure:
def infection_trajectory_chart(state="US") -> go.Figure:
"""Line chart data for the selected state.
# TODO: Population is hardcoded we can pull it from our BE
:params state: get the time series data for a particular state for confirmed, deaths, and recovered. If None, the whole US.
:params state: get the time series data for a particular state for
confirmed, deaths, and recovered. If None, the whole US.
"""

if state == "US":
Expand Down Expand Up @@ -69,7 +70,8 @@ def new_infection_trajectory_chart(state="US") -> go.Figure:

# <extra></extra> remove name from the end of the hover over text
template = (
"%{y:.0f} confirmed cases per 100,000 people<br>in %{text} <extra></extra>"
"%{y:.0f} confirmed cases per 100,000 people<br>in"
" %{text} <extra></extra>"
)

countries = ["Italy", "South Korea", "US"]
Expand Down Expand Up @@ -145,9 +147,12 @@ def new_infection_trajectory_chart(state="US") -> go.Figure:
"California": 39512223,
}

state_populations = pd.read_csv("components/state-population-est2019.csv")
state_populations = pd.read_csv(
"ncov19_dash/components/state-population-est2019.csv"
)
state_populations = state_populations[["Region", "2019"]]
state_populations["2019"] = state_populations["2019"].str.replace(",", "")
state_populations["2019"] = state_populations["2019"].\
str.replace(",", "")
state_populations["2019"] = state_populations["2019"].fillna(0)
state_populations["2019"] = state_populations["2019"].astype(int)

Expand All @@ -172,8 +177,10 @@ def new_infection_trajectory_chart(state="US") -> go.Figure:
{"Date": "3/1/20", "Confirmed": 1338},
]
data = pd.DataFrame(backup)
temp_data = data["Confirmed"].to_frame(REVERSE_STATES_MAP[comp_state])

temp_data = data["Confirmed"].to_frame(
REVERSE_STATES_MAP[comp_state]
)

# convert data to per 100K and filter where greater than 1/100K
population = populations[REVERSE_STATES_MAP[comp_state]]
temp_data = temp_data[
Expand Down Expand Up @@ -203,7 +210,8 @@ def new_infection_trajectory_chart(state="US") -> go.Figure:
fig = go.Figure()

template = (
"%{y:.0f} confirmed cases per 100,000 people<br>in %{text} <extra></extra>"
"%{y:.0f} confirmed cases per 100,000 people<br>in"
" %{text} <extra></extra>"
)

# reversing list so the line for input state is on top
Expand Down Expand Up @@ -257,7 +265,7 @@ def new_infection_trajectory_chart(state="US") -> go.Figure:
font=dict(family="Roboto, sans-serif", size=10, color="#f4f4f4"),
yaxis_title="Cases per 100k People",
)

del merged
gc.collect()

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import requests
import pandas as pd

from ncov19_dash.utils import config
from ncov19_dash import config


try:
Expand Down
2 changes: 1 addition & 1 deletion ncov19_dash/components/news_feed.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import dash_html_components as html

from ncov19_dash.cache import server_cache
from ncov19_dash.utils import config
from ncov19_dash import config


@server_cache.memoize(timeout=900)
Expand Down
2 changes: 1 addition & 1 deletion ncov19_dash/components/scatter_mapbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import plotly.graph_objects as go

from ncov19_dash.utils import STATES_COORD
from ncov19_dash.utils import config
from ncov19_dash import config


px.set_mapbox_access_token(config.MAPBOX_ACCESS_TOKEN)
Expand Down
2 changes: 1 addition & 1 deletion ncov19_dash/components/stats_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import dash_html_components as html
import dash_table
from ncov19_dash.utils import REVERSE_STATES_MAP
from ncov19_dash.utils import config
from ncov19_dash import config


def stats_table(state="US"):
Expand Down
2 changes: 1 addition & 1 deletion ncov19_dash/components/twitter_feed.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import dash_bootstrap_components as dbc
import dash_html_components as html
from ncov19_dash.cache import server_cache
from ncov19_dash.utils import config
from ncov19_dash import config
from ncov19_dash.utils import STATES_COORD


Expand Down
22 changes: 21 additions & 1 deletion ncov19_dash/utils/config.py → ncov19_dash/config.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import os
import pathlib
from decouple import config


class StagingConfig(object):
PACKAGE_ROOT = pathlib.Path(__file__).resolve().parent.parent


class StagingConfig:
"""Base config, uses staging API"""

DEBUG = True
Expand Down Expand Up @@ -45,3 +50,18 @@ class ProductionConfig(StagingConfig):

# MapBox Style
MAPBOX_STYLE = config("MAPBOX_PRODUCTION_STYLE")


# Set default config to ProductionConfig unless STAGING environment
# is set to true on Linux `export STAGING=True` or Windows Powershell
# `$Env:STAGING="True"`. Using os.environ directly will throw errors
# if not set.
def get_config():
STAGING = os.getenv("STAGING") or "False"

if STAGING == "True":
return StagingConfig

return ProductionConfig

config = get_config()
Loading

0 comments on commit e0b808e

Please sign in to comment.