Export Dataframe from python / pandas to javascript - javascript

I am trying to export data from python to javascript.
I have a python pandas dataframe df that I export to a file "formatted.json" using the following code
with open('../data/formatted.json', 'w') as outfile:
df.to_json(outfile)
Here's an excerpt of the ../data/formatted.json file:
{"game_id":{"0":"bda33adca828c09dc3cac3a856aef176","1":"03f67404824aee9be3cba7bc3a2a3499","2":"658843f757b400ecbc5587e8ed3e5521","3":"51e4e3d8fe4d2ecf7f926b5049696f0e","4":"d2f82f3973ced311faac8c6bd90b16b9","5":"c1e42fa78b9a527487211c2dfccad8fb","6":"ee25ac1aa64a6b33cfd7d42881e4f7b9"}}
I then try to import this data in javascript to read into my react component using
import oddsdata from '../data/formatted.json'
//returns error
Module not found: Can't resolve '../data/formatted.json'
I think the problem is because i dont have an export in my formatted.json file? How can I configure either the python export or the js import to overcome this issue?
the js file is saved in src/betting/betinterface/betinterface.js and the data is saved in src/data/formatted.json

The link to the data file formatted.json is not correct as per our discussion in the comment.
You should change it to
import oddsdata from '../../data/formatted.json'

Related

Export tables as .xlsx format in vue 3

I have tried to find a library that is used to export an HTML table to .xlsx format in Vue 3. But The libraries don't work properly for Vue 3.
npm install -save vue3-xlsx
I have used it but it imports components from ../../dist/vue3-xlsx.cjs.prod.js
import { XlsxRead, XlsxTable, XlsxSheets, XlsxJson, XlsxWorkbook, XlsxSheet, XlsxDownload } from "../../dist/vue3-xlsx.cjs.prod.js";
and it create module not found error.
https://www.npmjs.com/package/vue3-xlsx
any help would be much appreciated
Try do the import like this instead: import { XlsxWorkbook, XlsxSheet, XlsxDownload } from 'vue3-xlsx'

TypeError: CryptoJS is undefined

Im working on a project in ReactJS and I want to implement the AES function from CyptoJS library. But when I perform the event that triggers the use of the AES ecrypting I recieve the following error: TypeError: crypto_js__WEBPACK_IMPORTED_MODULE_1__.CryptoJS is undefined
import React, {useState} from 'react';
import {CryptoJS} from 'crypto-js';
export function Register(){
var body = /* a JSON with information from the register */
var encyMssg = CryptoJS.AES.encrypt(JSON.stringify(body), "key").toString();
return(/* HTML COMPONENT*/);
}
The error displays in the var encyMssg line and this script is written in a .jsx file. Any kind of solution? Thank you for your time.
SOLVED thanks a lot. The problem as several of you said was the {} in the import line; the correcto way to import the libray was:
import CryptoJS from 'crypto-js';
Make sure you have to installed crypto-js using
npm install crypto-js
Then in your JS file import module you want to use.
import aes from 'crypto-js/aes';
Now you can aes like below.
aes(value, key).toString()
Here is working stackblitz example.
React Crypto AES StackBlitz

Is there a way to import .html file from JavaScript?

I have the following import statements in my Cloudflare Worker project, to import a html file as a string constant :
import userAgentJsch from './userAgentJsch';
import { parse } from 'cookie';
import test from '.test.html';
It fails with the error:
Module not found: Error: Can't resolve '.test.html' in
I was reading about needing to configure my webpack.config.js file, but I don't have such a file.
Can you please help? I just want to keep my html file separated from the js files, but I can't find a solution I can understand.
View folder structure is used for rendering templates no doubts
to read files in nodejs use this :
const fs = require("fs");
const files = fs.readFileSync("new.html");
and to import
var htmlContent = require('path/to/html/file.html');

Import a mock JSON file for Jest/Axios upload test

I am looking to test Axios functionality of a method which posts a file using Jest.
As part of this I would like to POST a local mocked JSON file, is that possible?
I can't see any way to require or import a JSON file for uploading in a POST.
// import jsonFileMock from "../__mocks__/jsonFileMock.json";
import jsonFileMock = require("../__mocks__/jsonFileMock.json");
const apiResponse = await someApiUpload(jsonFileMock); // Doesn't work
You're mixing ES6 import with commonjs require. Try
import * as jsonFileMock from "../__mocks__/jsonFileMock.json"
A json file can't have an export statement, so thats why you have to use the * as term. You can also just fetch it:
fetch('../__mocks__/jsonFileMock.json')
.then( async data => await someApiUpload(data) )
More about importing a json file can be found in the question How to import a json file in ecmascript 6?

"<type>" is not exported from "./path"

I'm trying to import a named value from one file and from that other I just export it as it should be but still some errors. I'm retrieving it from the current directory.
export type Homes = { ....
import {Homes} from './api';
I don't think it's relevant but, I import from js file and export from ts file.
A Javascript file cannot import a type, as a type is a purely Typescript concept that does not correspond to a Javascript object (unlike, say, an exported class or function).

Categories