The below Java code will extract the historical stock data from yahoo finance, but the output file is a CSV file that is being read and printed on the screen line by line.
But I want to do the same thing in React native.
It would be helpful if you guys can help me with the code for the same in react native.
String link="https://query1.finance.yahoo.com/v7/finance/download/TSLA?period1=1563992913&period2=1595615313&interval=1d&events=history";
URL url = new URL(link);
URLConnection urlConn = url.openConnection();
InputStreamReader inStream = new InputStreamReader(urlConn.getInputStream());
BufferedReader buf = new BufferedReader(inStream);
String line =buf.readLine();
while(line != null) {
System.out.println(line);
line=buf.readLine();
}
This is the data inside the link :
Date,Open,High,Low,Close,Adj Close,Volume
2019-07-24,259.170013,266.070007,258.160004,264.880005,264.880005,11072800
2019-07-25,233.500000,234.500000,225.550003,228.820007,228.820007,22418300
2019-07-26,226.919998,230.259995,222.250000,228.039993,228.039993,10027700
2019-07-29,227.089996,235.940002,226.029999,235.770004,235.770004,9273300
You could use a library such as Papa parse to do exactly this. It will also parse the data for you.
If we take the example from here, you could do something like this (assuming you would also like to print the data to the console):
import Papa from "papaparse"
const link = "https://query1.finance.yahoo.com/v7/finance/download/TSLA?period1=1563992913&period2=1595615313&interval=1d&events=history"
Papa.parse(link, {
download: true,
complete: results => console.log(results),
})
Related
I would like to upload local image file and extract text from it. I followed the below link and it works as expected when I pass URL. https://learn.microsoft.com/en-us/azure/developer/javascript/tutorial/static-web-app/add-computer-vision-react-app
I managed to configure for local image and get the base64 encoded dataURL of the uploaded image. But when I pass base64 encoded dataURL to Computer Vision API , it says "Input data is not a valid image" (POST 400 status code). I am getting error in the line that is shown below:
const analysis = await computerVisionClient.analyzeImage(urlToAnalyze, { visualFeatures });
The code I have included for handling local image:
const handleChange = (e) => {
var file = e.target.files[0];
var reader = new FileReader();
reader.onloadend = function()
{
setFileSelected(reader.result) // this is the base64 encoded dataurl
}
reader.readAsDataURL(file);
}
In computerVision.js file, I have changed the 'contentType' in header as below.
const computerVisionClient = new ComputerVisionClient(
new ApiKeyCredentials({ inHeader: {'Ocp-Apim-Subscription-Key': key, 'Content-Type': 'application/octet-stream'} }), endpoint);
I tried replacing client.read() with readTextInStream() as per docs in computerVision.js (please refer above link), but still throws error.
May I know why I get the error "Input data is not a valid image" ? Thanks.
Here is the link for input requirements.
There is a brand new online portal provided by Microsoft https://preview.vision.azure.com/demo/OCR
The advantage is that it will directly list your available resources so you just have to pick the right one, then you test, and there are also some samples.
I am developing a Flutter App, and I wish to save a list of objects (Contacts) to the app directory.
As suggested in the Cookbook, I use the path_provider package and the File class of Dart.
Getting to write the objects was easy (I think - I didn't get any error). Here is the code I used for writing the list:
Future<void> writeContact(List<Contact> contacts) async {
final directory = await getApplicationDocumentsDirectory();
final file = File('${directory.path}/contacts.txt');
// Write the file
IOSink sink = file.openWrite(mode: FileMode.write);
sink.write(contacts);
sink.close();
}
The question is - how to I read this file and populate the List of Contacts?
Thanks, Gal.
I want users to be able to download files uploaded by them. The problem is, certein files (like .pdf) are getting open in the browser, not downloaded. Is there any solution to just download a file in Flutter Web without opening it in the web browser?
Code that I'm using:
final anchor = AnchorElement(
href: url)
..setAttribute("download", fileName)
..click();
Following this answer, you can generate url from a string or from a list of int (bytes of file). Here is a short snipper that should work (from the answer above):
import 'dart:convert';
import 'dart:html' as html; // or package:universal_html/prefer_universal/html.dart
final text = 'this is the text file';
// prepare
final bytes = utf8.encode(text);
final blob = html.Blob([bytes]);
final url = html.Url.createObjectUrlFromBlob(blob);
final anchor = html.document.createElement('a') as html.AnchorElement
..href = url
..style.display = 'none'
..download = 'some_name.txt';
html.document.body.children.add(anchor);
// download
anchor.click();
// cleanup
html.document.body.children.remove(anchor);
html.Url.revokeObjectUrl(url);
Also, universal_html package is very handy to use html on all Flutter platforms. Using dart:html in a Flutter mobile app will make compiling fail.
I integrated mxGraph(mxgraph npm package) in my react app, so when I trying to load graph using xml, I am getting an error in console
TypeError: geo.clone is not a function
The same I am doing in single html file and it's working.
I investigated and found that the mxCell in react app is different from the html one.
In case of HTML there is filled geometry prop instead of react(check screens below)
Сan someone help me to decode xml correctly?
Decoded mxCell from single HTML console: https://monosnap.com/file/yAHAi29zFGFpauqU2RtDcvmfPpZ0YJ
Decoded mxCell from React app console: https://monosnap.com/file/0XxPwyEracX7hMCnMHckAmI8Rl6OEh
Source code from React component:
const graph = new mx.mxGraph(this.automationRef.current)
new mx.mxRubberband(graph);
const xml = '<root>...</root>';
const doc = mx.mxUtils.parseXml(xml);
const codec = new mxCodec(doc);
let elt = doc.documentElement.firstChild;
const cells = [];
while (elt != null){
const cell = codec.decodeCell(elt)
cells.push(cell);
graph.refresh();
elt = elt.nextSibling;
}
graph.addCells(cells);
Found the issue.
Here's the solution:
https://github.com/jgraph/mxgraph/issues/301#issuecomment-514284868
Quote:
you should add
window['mxGraphModel'] = mxGraphModel;
window['mxGeometry'] = mxGeometry;
before
let doc = mxUtils.parseXml(xml);
let codec = new mxCodec(doc);
codec.decode(doc.documentElement, graph.getModel());'
I found that the decode method resolve xml need windows param
End quote
I need to download and view file (if possible, for Image, PDF, etc) using Flutter.
My problem is, the file that I want to download is Base64 String.
How can I achieve that using Flutter??
Following is the code snippet to decode base64 string and save it as a file on the local device. please note in terms of viewing files, image and pdf would require different libraries.
Future<String> _createFileFromString() async {
final encodedStr = "put base64 encoded string here";
Uint8List bytes = base64.decode(encodedStr);
String dir = (await getApplicationDocumentsDirectory()).path;
File file = File(
"$dir/" + DateTime.now().millisecondsSinceEpoch.toString() + ".pdf");
await file.writeAsBytes(bytes);
return file.path;
}
Google Chrome does not let open tab with Url base64 encode, like "data:application/pdf;base64,JVBERi0xLjQKJeLj..." with javascript, so you can not open url with url_launcher, but you can download file.
Here is my code:
import 'dart:html' as html;
...
Future<void> downloadFile() {
final urlString = "data:application/pdf;base64,JVBERi0xLjQKJeLj...";
html.AnchorElement anchorElement = html.AnchorElement(href:urlString);
anchorElement.download = urlString;
anchorElement.click();
}