Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed CSV Export format from news GUI #5875

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 20 additions & 11 deletions frontend-components/tables/src/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export function formatNumberNoMagnitude(value: number | string) {
if (typeof value === "string") {
const suffix = value.replace(/[^a-zA-Z]/g, "").trim();
const magnitude = ["", "K", "M", "B", "T"].indexOf(
suffix.replace(/\s/g, ""),
suffix.replace(/\s/g, "")
);
value =
Number(value.replace(/[^0-9.]/g, "").trim()) *
Expand All @@ -24,7 +24,7 @@ export function formatNumberMagnitude(value: number | string, column?: string) {
if (value % 1 !== 0) {
const decimalPlaces = Math.max(
2,
value.toString().split(".")[1]?.length || 0,
value.toString().split(".")[1]?.length || 0
);
const toFixed = Math.min(4, decimalPlaces);
if (value < 5) {
Expand Down Expand Up @@ -61,20 +61,20 @@ export function formatNumber(value: number) {

export function includesDateNames(column: string) {
return ["date", "day", "time", "timestamp", "year"].some((dateName) =>
column?.toLowerCase().includes(dateName),
column?.toLowerCase().includes(dateName)
);
}

export function includesPriceNames(column: string) {
return ["price", "open", "close"].some((priceName) =>
column?.toLowerCase().includes(priceName),
column?.toLowerCase().includes(priceName)
);
}

function loadingOverlay(message?: string, is_close?: boolean) {
const loading = window.document.getElementById("loading") as HTMLElement;
const loading_text = window.document.getElementById(
"loading_text",
"loading_text"
) as HTMLElement;
return new Promise((resolve) => {
if (is_close) {
Expand Down Expand Up @@ -113,7 +113,7 @@ export const fuzzyFilter = (
row: any,
columnId: string,
value: string,
addMeta: any,
addMeta: any
): any => {
const itemRank = rankItem(row.getValue(columnId), value);
addMeta(itemRank);
Expand Down Expand Up @@ -199,7 +199,7 @@ const getNewFileHandle = ({
export const saveToFile = (
blob: Blob,
fileName: string,
fileHandle?: FileSystemFileHandle,
fileHandle?: FileSystemFileHandle
) => {
try {
if (fileHandle === null) {
Expand Down Expand Up @@ -227,16 +227,25 @@ export async function downloadData(
type: "csv" | "xlsx",
columns: any,
data: any,
downloadFinished: (changed: boolean) => void,
downloadFinished: (changed: boolean) => void
) {
const headers = columns;
const rows = data.map((row: any) =>
headers.map((column: any) => row[column]),
headers.map((column: any) => row[column])
);
const csvData = [headers, ...rows];

if (type === "csv") {
const csvContent = csvData.map((e) => e.join(",")).join("\n");
// const csvContent = csvData.map((e) => e.map((field) => `"${field.toString().replace(/"/g, '""')}"`).join(",")).join("\n");
const csvContent = csvData
.map((e, index) =>
[
`"${index}"`,
...e.map((field) => `"${field.toString().replace(/"/g, '""')}"`),
].join(",")
)
.join("\n");

const blob = new Blob([csvContent], { type: "text/csv;charset=utf-8;" });
const filename = `${window.title}.csv`;

Expand Down Expand Up @@ -287,7 +296,7 @@ export async function downloadData(

export async function downloadImage(
id: string,
downloadFinished: (change: boolean) => void,
downloadFinished: (change: boolean) => void
) {
const table = document.getElementById(id);
const filename = `${window.title}.png`;
Expand Down
3 changes: 3 additions & 0 deletions openbb_terminal/common/feedparser_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,12 @@ def get_news(
return pd.DataFrame()
else:
df = pd.DataFrame(data.entries, columns=["title", "link", "published"])

df["published"] = pd.to_datetime(df["published"])
df = df.sort_values(by=[sort], ascending=False)
df = df[["published", "title", "link"]]
df.columns = ["Date", "Description", "URL"]
df = df[:limit]

df.reset_index(inplace=True)
return df
Loading
Loading