Git Product home page Git Product logo

papa-parse's Introduction

papa-parse GitHub release Published on webcomponents.org styled with prettier

<!-- download and parse into JSON -->
<papa-parse auto
  url="https://rawgit.com/PolymerVis/papa-parse/master/demo/MOCK_DATA.csv"
  rows="{{rows}}"></papa-parse>

<!-- render as a table -->
<h4>Parsed CSV table:</h4>
<table>
  <template is="dom-repeat" items="[[rows]]" as="row">
    <tr>
      <template is="dom-repeat" items="[[row]]">
        <td>[[item]]</td>
      </template>
    </tr>
  </template>
</table>

<!-- unparse JSON into string -->
<papa-unparse header
  json-array="[[rows]]"
  csv-str="{{csvStr}}"></papa-unparse>

<!-- csv string output -->
<h4>Csv string</h4>
<pre>[[csvStr]]</pre>

Installation

bower install --save PolymerVis/papa-parse

Documentation and demos

More details @ webcomponents.org.

Disclaimers

PolymerVis is a personal project and is NOT in any way affliated with Papaparse, Polymer or Google.

papa-parse

papa-parse is a Polymer 2.0 element to parse CSV files into JSON object(s) with Papa parse.

papa-parse can download and parse a csv file via url, or from raw csv strings via raw, and File object via file. If the auto flag is set, papa-parse will automatically start the job, otherwise a manual call to the function start will be needed.

Parse from URL.

<papa-parse auto url="MOCK_DATA.csv" rows="{{rows}}"></papa-parse>

Parse from raw csv String.

<papa-parse auto raw="[[SomeCsvString]]" rows="{{rows}}"></papa-parse>

Parse from FileReader

<input id="selectfile" type="file"></input>
<papa-parse auto file="[[file]]" rows="{{rows}}"></papa-parse>
this.$.selectfile
  .addEventListener('change', function(e) {
    this.file = e.target.files[0];
  });

There are 2 possible outputs for rows - an array of array of values, or an array of objects. An array of array is returned if the header flag is not set, and an array of objects otherwise, where each object is a map comprising of the column name and its corresponding value for the row (e.g. {col1: value1, col2: value2}).

Handling big files

Parsing is synchronous and may block the UI thread (i.e. freeze the screen) if the csv file is big. Parsing with a web-worker via worker flag is recommended for big file. However, note that papaparse.min.js should not be bundled during the build process if web-worker is required as the web-worker needs to load the script. You will also need to correctly reference the location of the script via the script-path attribute.

You should not use relative path if parsing from url, as the web-worker may load from an incorrect path.

<papa-parse auto header worker stream
  script-path="../../papaparse/papaparse.min.js"
  fields="{{fields}}"
  url="https://some.domain/some.csv"
  on-stream="handleRecord"></papa-parse>
function handleRecord(e, {data, meta, errors}) {
  console.log(data);
}

Streaming via stream flag is also recommended for big file (stream and worker are not dependent on each other). However, when stream flag is set, rows attribute will only return an empty array as none of the records will be persisted. Instead, papa-parse will emit an stream event of the form {{data: Array[]|Object[], meta: Object, errors: Array, parser: Object}} for each record parsed.

<button onclick="javascript:start()">[[buttonLabel]]</button>

<papa-parse id="parser"
  stream
  url="MOCK_DATA.csv"
  rows="{{rows}}"
  on-stream="handleRecord"></papa-parse>
function start() {
  app.$.parser.start();
}

function handleRecord(e, {data, meta, errors}) {
  alert(JSON.stringify(data));
  app.$.parser.pause();
}

papa-unparse

papa-unparse is a Polymer 2.0 element to convert JSON arrays or objects into CSV strings with Papa parse.

Basic usage
For array of objects, e.g. [{col1: 1, col2: 2}, {col1: 3, col2: 4}]

<papa-unparse header json-array="[[rows]]"></papa-unparse>

For array of arrays, e.g. [[1,2], [3,4]]

<papa-unparse json-array="[[rows]]" fields="[[fields]]"></papa-unparse>

Download as CSV file

<papa-unparse header json-array="[[rows]]"></papa-unparse>
<button onclick="javascript:download">Download</button>
function download() {
  document.querySelector('papa-unparse').downloadCsv('somefile.csv');
}

Other options

<papa-unparse
  header
  quotes
  quoteChar="'"
  delimiter="|"
  newline="\n"
  json-array="[[rows]]"></papa-unparse>

papa-parse's People

Contributors

eterna2 avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

papa-parse's Issues

Action required: Greenkeeper could not be activated 🚨

🚨 You need to enable Continuous Integration on all branches of this repository. 🚨

To enable Greenkeeper, you need to make sure that a commit status is reported on all branches. This is required by Greenkeeper because it uses your CI build statuses to figure out when to notify you about breaking changes.

Since we didn’t receive a CI status on the greenkeeper/initial branch, it’s possible that you don’t have CI set up yet. We recommend using Travis CI, but Greenkeeper will work with every other CI service as well.

If you have already set up a CI for this repository, you might need to check how it’s configured. Make sure it is set to run on all new branches. If you don’t want it to run on absolutely every branch, you can whitelist branches starting with greenkeeper/.

Once you have installed and configured CI on this repository correctly, you’ll need to re-trigger Greenkeeper’s initial pull request. To do this, please delete the greenkeeper/initial branch in this repository, and then remove and re-add this repository to the Greenkeeper App’s white list on Github. You'll find this list on your repo or organization’s settings page, under Installed GitHub Apps.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.