Module:Tabular data
![]() | This module is rated as beta, and is ready for widespread use. It is still new and should be used with some caution to ensure the results are as expected. |
![]() | This module is subject to page protection. It is a highly visible module in use by a very large number of pages, or is substituted very frequently. Because vandalism or mistakes would affect many pages, and even trivial editing might cause substantial load on the servers, it is protected from editing. |
This module provides basic functions for interacting with tabular data on Wikimedia Commons.
cell
Returns the value of the cell at the given row index and column name.
Usage: {{#invoke:Tabular data|cell|Page name.tab|output_row=Index of row to output|output_column=Name of column to output}}
A row index of 1
refers to the first row in the table. A row index of -1
refers to the last row in the table. It is an error to specify a row index of 0
.
Examples
Latest death toll in c:Data:COVID-19 cases in Santa Clara County, California.tab (regardless of when the table was last updated):
ERROR: Invalid module function: cell
lookup
Returns the value of the cell(s) in one or more output columns of the row matching the search key and column.
This function is reminiscent of LOOKUP()
macros in popular spreadsheet applications, except that the search key must match exactly. (On the other hand, this means the table does not need to be sorted.)
Usage: {{#invoke:Tabular data|lookup|Page name.tab|search_value=Value to find in column|search_column=Name of column to search in|output_column=Name of column to output|output_column2=Name of another column to output|output_columnn=…|output_format=String format to format the output}}
If multiple columns are output without an explicit string format, this function formats the output as a human-readable list.
Some may find {{Tabular query}} (which uses this module) an intuitive way to obtain cell data as it resembles a simple SQL query.
Parameters
|1=
- Page name on Commons with extension but no namespace
|search_value=
or|search_pattern=
- Value to find or pattern to match in column
|search_column=
- Name of column to search in
|occurrence=
- Index of the match to output in case of multiple matching rows. A row index of
1
refers to the first matching row. A row index of-1
refers to the last matching row. It is an error to specify a row index of0
. |output_column=
or|output_column1=
,|output_column2=
, ...- Names of columns to output
|output_format=
- String format to format the output
Examples
Total confirmed case count in c:Data:COVID-19 cases in Santa Clara County, California.tab on the day that the county issued a stay-at-home order:
ERROR: Invalid module function: lookup
The last day that a hundred or more patients with COVID-19 were in the hospital in c:Data:COVID-19 cases in Solano County, California.tab:
ERROR: Invalid module function: lookup
Total number of administrators on all Wikimedia wikis using c:Data:Wikipedia statistics/data.tab:
ERROR: Invalid module function: lookup
Number of administrators and users on all Wikimedia wikis using c:Data:Wikipedia statistics/data.tab:
ERROR: Invalid module function: lookup
Note: Wikipedia statistics are shown as an illustration only. In practice, there is a high-performance module {{NUMBEROF}}
to access Wikipedia statistics.
wikitable
Returns the entire data table as a (rather plain) table.
Usage: {{#invoke:Tabular data|wikitable|Page name.tab}}
Examples
COVID-19 statistics in Santa Clara County, California
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
{{#invoke:Tabular data|wikitable
|COVID-19 cases in Santa Clara County, California.tab}}
|
Implementation notes
The implementation of this function incorporates {{n/a}} (to represent null values), {{yes}} (true), and {{no}} (false). The templates themselves cannot be reused because they are incompatible with the mw.html
library, which builds the table using an HTML DOM instead of pure wikitext.
Internationalization
You can most likely port this template to a wiki in another language without making major modifications. The wikitable
function automatically localizes the table's description, column titles, and license name into the wiki's content language. It also formats numbers according to the content language. However, you should localize the cells representing true
, false
, and null
by changing the values in the messages
, bgColors
, and colors
variables to match the wiki's own {{yes}}, {{no}}, and {{n/a}} templates, respectively.
See also
- {{NUMBEROF}}
- {{Last tab}} and {{Date tab}}
- sv:Template:Json2table – shows a complete table (based on sv:Module:Json2table, in turn based on a deleted module)
- Template:Wdtable row – fetches a table row from Wikidata in realtime
- Template:Wikidata list – uses a bot to periodically fetch a complete table from Wikidata
-- Example usage:
-- =p.wikitable(mw.getCurrentFrame():newChild{ args = { "COVID-19 Cases in Santa Clara County, California.tab" } })
local p = {}
local lang = mw.getContentLanguage()
function p.wikitable(frame)
local data = mw.ext.data.get(frame.args[1])
local datatypes = {}
local htmlTable = mw.html.create("table")
:attr("class", "wikitable sortable")
htmlTable
:tag("caption")
:wikitext(data.description)
local headerRow = htmlTable
:tag("thead")
:tag("tr")
for i, field in ipairs(data.schema.fields) do
headerRow
:tag("th")
:attr("scope", "col")
:wikitext(field.title)
datatypes[i] = field.type
end
local body = htmlTable:tag("tbody")
for i, record in ipairs(data.data) do
local row = body:tag("tr")
for j, datapoint in ipairs(record) do
local cell = row:tag("td")
if cell then
local formattedData = datapoint
if datatypes[j] == "number" then
formattedData = lang:formatNum(formattedData)
cell:attr("align", "right")
end
cell:wikitext(formattedData)
else
cell:wikitext(frame:expandTemplate {
title = "n/a",
})
end
end
end
local footer = htmlTable
:tag("tfoot")
:tag("tr")
:tag("td")
:attr("colspan", #data.schema.fields)
footer:wikitext(data.sources)
footer:tag("br")
local licenseText = mw.message.new("Jsonconfig-license",
mw.ustring.format("[%s %s]", data.license.url, data.license.text))
footer
:tag("i")
:wikitext(tostring(licenseText))
return htmlTable
end
return p