Skip to content

Commit

Permalink
wip round
Browse files Browse the repository at this point in the history
  • Loading branch information
Bjwebb committed Nov 19, 2024
1 parent b824e86 commit 124f806
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 14 deletions.
37 changes: 26 additions & 11 deletions dashboard/make_csv.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import humanitarian
import summary_stats
import timeliness
from ui.jinja2 import round_nicely

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -173,7 +174,11 @@ def main():
for row in forwardlooking.table():
writer.writerow(
[row["publisher_title"], row["publisher"]]
+ [year_column[year] for year_column in row["year_columns"] for year in forwardlooking.years]
+ [
round_nicely(year_column[year])
for year_column in row["year_columns"]
for year in forwardlooking.years
]
)

for tab in comprehensiveness.columns.keys():
Expand All @@ -191,10 +196,13 @@ def main():
writer.writerow(
[row["publisher_title"], row["publisher"]]
+ [
row[slug + "_valid"] if slug in row else "-"
round_nicely(row[slug + "_valid"]) if slug in row else "-"
for slug in comprehensiveness.column_slugs[tab]
]
+ [
round_nicely(row[slug]) if slug in row else "-"
for slug in comprehensiveness.column_slugs[tab]
]
+ [row[slug] if slug in row else "-" for slug in comprehensiveness.column_slugs[tab]]
+ ["Yes" if row["flag"] else "-"]
)
else:
Expand All @@ -207,10 +215,13 @@ def main():
writer.writerow(
[row["publisher_title"], row["publisher"]]
+ [
row[slug + "_valid"] if slug in row else "-"
round_nicely(row[slug + "_valid"]) if slug in row else "-"
for slug in comprehensiveness.column_slugs[tab]
]
+ [
round_nicely(row[slug]) if slug in row else "-"
for slug in comprehensiveness.column_slugs[tab]
]
+ [row[slug] if slug in row else "-" for slug in comprehensiveness.column_slugs[tab]]
)

logger.info("Generating summary_stats.csv")
Expand All @@ -223,7 +234,11 @@ def main():
for row in summary_stats.table():
# Write each row
writer.writerow(
[row["publisher_title"], row["publisher"]] + [row[slug] for slug, header in summary_stats.columns]
[row["publisher_title"], row["publisher"]]
+ [
row[slug] if header == "Publisher Type" else round_nicely(row[slug])
for slug, header in summary_stats.columns
]
)

logger.info("Generating humanitarian.csv")
Expand All @@ -250,11 +265,11 @@ def main():
row["publisher"],
row["publisher_type"],
row["num_activities"],
row["publishing_humanitarian"],
row["humanitarian_attrib"],
row["appeal_emergency"],
row["clusters"],
row["average"],
round_nicely(row["publishing_humanitarian"]),
round_nicely(row["humanitarian_attrib"]),
round_nicely(row["appeal_emergency"]),
round_nicely(row["clusters"]),
round_nicely(row["average"]),
]
)

Expand Down
8 changes: 5 additions & 3 deletions dashboard/ui/jinja2.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@
import timeliness


def round_nicely(val, ndigits=2):
def round_nicely(val, ndigits=0):
"""Round a float, but remove the trailing .0 from integers that python insists on"""
if int(val) == float(val):
if val == "-":
return val
val = round(float(val), ndigits)
if val == int(val):
return int(val)
return round(float(val), ndigits)


def xpath_to_url(path):
Expand Down

0 comments on commit 124f806

Please sign in to comment.