Skip to content

Read a CSV file ​

Task: show the active customers of a CSV file, oldest first.

yaml
# Recipe: read a CSV file, typed, filtered and sorted — no database.
paths:
  components: ./components
text
id,name,age,active
1,Ana,28,true
2,Bruno,35,true
3,Carla,42,false
4,Davi,19,yes

Declared columns are typed (true, 1, yes and on are a true boolean); the others come as text. q:transform runs its operations in order.

xml
<q:component name="Customers">
  <!-- The path is relative to the app's folder. Declared columns are typed;
       the others come as text. -->
  <q:data name="customers" source="import/customers.csv" type="csv">
    <q:column name="id" type="integer" />
    <q:column name="age" type="integer" />
    <q:column name="active" type="boolean" />
    <q:transform>
      <q:filter condition="active" />
      <q:sort by="age" order="desc" />
    </q:transform>
  </q:data>

  <ui:window title="Active customers">
    <ui:table source="{customers}">
      <ui:column key="name" label="Name" />
      <ui:column key="age" label="Age" />
    </ui:table>
  </ui:window>
</q:component>
xml
<q:test name="the active customers, oldest first" page="/">
  <test:visit />
  <test:expect text="Bruno 35 Ana 28 Davi 19" />
  <test:expect no-text="Carla" />
</q:test>
text
tests/customers.test.q
  PASS  the active customers, oldest first
1 passed, 0 failed

See DATA-1 and DATA-3, and Data Import.

MIT Licensed · Built with VitePress