Skip to content

Commit

Permalink
Merge pull request #4242 from owid/filter-download-columns
Browse files Browse the repository at this point in the history
✨ Filter for only active columsn in grapher data download
  • Loading branch information
danyx23 authored Dec 3, 2024
2 parents bac04dd + ff311ee commit d251b43
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 8 deletions.
26 changes: 20 additions & 6 deletions packages/@ourworldindata/core-table/src/OwidTable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -606,12 +606,26 @@ export class OwidTable extends CoreTable<OwidRow, OwidColumnDef> {
}

// Give our users a clean CSV of each Grapher. Assumes an Owid Table with entityName.
toPrettyCsv(useShortNames: boolean = false): string {
return this.dropColumns([
OwidTableSlugs.entityId,
OwidTableSlugs.time,
OwidTableSlugs.entityColor,
])
toPrettyCsv(
useShortNames: boolean = false,
activeColumnSlugs: string[] | undefined = undefined
): string {
let table
if (activeColumnSlugs?.length) {
table = this.select([
OwidTableSlugs.year,
OwidTableSlugs.day,
this.entityNameSlug,
...activeColumnSlugs,
])
} else {
table = this.dropColumns([
OwidTableSlugs.entityId,
OwidTableSlugs.time,
OwidTableSlugs.entityColor,
])
}
return table
.sortBy([this.entityNameSlug])
.toCsvWithColumnNames(useShortNames)
}
Expand Down
11 changes: 9 additions & 2 deletions packages/@ourworldindata/grapher/src/modal/DownloadModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ export interface DownloadModalManager {
showAdminControls?: boolean
isSocialMediaExport?: boolean
isPublished?: boolean
activeColumnSlugs?: string[]
}

interface DownloadModalProps {
Expand Down Expand Up @@ -432,13 +433,17 @@ interface DataDownloadContextClientSide extends DataDownloadContextBase {
// Only needed for local CSV generation
table: OwidTable
transformedTable: OwidTable
activeColumnSlugs: string[] | undefined
}

const createCsvBlobLocally = async (ctx: DataDownloadContextClientSide) => {
const csv =
ctx.csvDownloadType === CsvDownloadType.Full
? ctx.table.toPrettyCsv(ctx.shortColNames)
: ctx.transformedTable.toPrettyCsv(ctx.shortColNames)
? ctx.table.toPrettyCsv(ctx.shortColNames, ctx.activeColumnSlugs)
: ctx.transformedTable.toPrettyCsv(
ctx.shortColNames,
ctx.activeColumnSlugs
)

return new Blob([csv], { type: "text/csv;charset=utf-8" })
}
Expand Down Expand Up @@ -765,13 +770,15 @@ export const DownloadModalDataTab = (props: DownloadModalProps) => {
table: props.manager.table ?? BlankOwidTable(),
transformedTable:
props.manager.transformedTable ?? BlankOwidTable(),
activeColumnSlugs: props.manager.activeColumnSlugs,
}),
[
props.manager.baseUrl,
props.manager.displaySlug,
props.manager.queryStr,
props.manager.table,
props.manager.transformedTable,
props.manager.activeColumnSlugs,
]
)

Expand Down

0 comments on commit d251b43

Please sign in to comment.