Writing HTML files
In these examples you will learn how to generate HTML reports based on your data.
Start by importing scalaql
:
import scalaql._
// Docs classes
import scalaql.docs.DocUtils._
// Imports for examples
import java.nio.file.Paths
We'll use the following CSV dataset:
import scalaql.docs.CsvData._
val dir = Paths.get("docs/src/main/resources/annual-enterprise-survey-2020/")
// dir: java.nio.file.Path = docs/src/main/resources/annual-enterprise-survey-2020
val csvInput =
csv
.read[EnterpriseSurvey]
// In this case it's required to provide correct naming.
.option(Naming.SnakeCase)
.directory(dir, globPattern = "**/*.csv")
// csvInput: Iterable[EnterpriseSurvey] = Vector(
// EnterpriseSurvey(
// year = 2018,
// industryCodeAnzsic = "O",
// industryNameAnzsic = "Public Administration and Safety",
// rmeSizeGrp = "d_10-19",
// variable = "Sales, government funding, grants and subsidies",
// value = "78",
// unit = "DOLLARS(millions)"
// ),
// EnterpriseSurvey(
// year = 2018,
// industryCodeAnzsic = "O",
// industryNameAnzsic = "Public Administration and Safety",
// rmeSizeGrp = "d_10-19",
// variable = "Total income",
// value = "78",
// unit = "DOLLARS(millions)"
// ),
// EnterpriseSurvey(
// year = 2018,
// industryCodeAnzsic = "O",
// industryNameAnzsic = "Public Administration and Safety",
// rmeSizeGrp = "d_10-19",
// variable = "Total expenditure",
// value = "72",
// unit = "DOLLARS(millions)"
// ),
// EnterpriseSurvey(
// year = 2018,
// industryCodeAnzsic = "O",
// industryNameAnzsic = "Public Administration and Safety",
// rmeSizeGrp = "d_10-19",
// variable = "Operating profit before tax",
// value = "6",
// unit = "DOLLARS(millions)"
// ),
// EnterpriseSurvey(
// year = 2018,
// industryCodeAnzsic = "O",
// industryNameAnzsic = "Public Administration and Safety",
// rmeSizeGrp = "d_10-19",
// variable = "Total assets",
// value = "39",
// unit = "DOLLARS(millions)"
// ),
// EnterpriseSurvey(
// year = 2018,
// industryCodeAnzsic = "O",
// ...
And then generate the following report:
case class ByIndustry(
industry: String,
totalProfitByIndustry: BigDecimal,
avgProfitByIndustry: BigDecimal)
case class ByYear(
year: Int,
totalProfitPerYear: BigDecimal,
avgProfitPerYear: BigDecimal,
byIndustry: List[ByIndustry])
val reportData: Query[From[EnterpriseSurvey], ByYear] =
select[EnterpriseSurvey]
.groupBy(_.year)
.aggregate { (year, data) =>
val byIndustry = data.report(_.industryNameAnzsic) { (industry, data) =>
(
data.sumBy(_.decimalValue) &&
data.avgBy(_.decimalValue)
).map { case (total, avg) => ByIndustry(industry, total, avg) }
}
(
data.sumBy(_.decimalValue) &&
data.avgBy(_.decimalValue) &&
byIndustry
).map { case (total, avg, byIndustry) =>
ByYear(year, total, avg, byIndustry)
}
}
// reportData: Query[From[EnterpriseSurvey], ByYear] = FROM(CsvData::EnterpriseSurvey) -> GROUP BY(Int) -> AGGREGATE(MdocSession::App::ByYear)
This is how it could be written as HTML file:
reportData
.foreach(
html
.write[ByYear]
.file(Paths.get("docs/target/report.html"))
)
.run(
from(csvInput)
)
It will produce the following report:
year | totalProfitPerYear | avgProfitPerYear | industry | totalProfitByIndustry | avgProfitByIndustry |
---|---|---|---|---|---|
2014 | 17617937.0 | 11381.09625322997416020671834625323 | |||
Rental, Hiring and Real Estate Services | 966078.0 | 11926.88888888888888888888888888889 | |||
Mining | 127488.0 | 1573.925925925925925925925925925926 | |||
Other Services | 230631.0 | 2847.296296296296296296296296296296 | |||
Administrative and Support Services | 299912.0 | 3702.617283950617283950617283950617 | |||
Health Care and Social Assistance | 459585.0 | 5673.888888888888888888888888888889 | |||
Manufacturing | 1328028.0 | 16395.40740740740740740740740740741 | |||
Transport, Postal and Warehousing | 446371.0 | 5510.753086419753086419753086419753 | |||
Agriculture, Forestry and Fishing | 1019889.0 | 12591.22222222222222222222222222222 | |||
Electricity, Gas, Water and Waste Services | 342154.0 | 4224.123456790123456790123456790123 | |||
Retail Trade | 873235.0 | 10780.67901234567901234567901234568 | |||
Public Administration and Safety | 34866.0 | 430.4444444444444444444444444444444 | |||
Financial and Insurance Services | 2444200.0 | 30175.30864197530864197530864197531 | |||
Accommodation and Food Services | 392687.0 | 4847.987654320987654320987654320988 | |||
Information Media and Telecommunications | 224878.0 | 2776.271604938271604938271604938272 | |||
Professional, Scientific and Technical Services | 679487.0 | 8388.728395061728395061728395061728 | |||
All Industries | 5897233.0 | 655248.1111111111111111111111111111 | |||
Education and Training | 134125.0 | 1655.864197530864197530864197530864 | |||
Construction | 692033.0 | 8543.617283950617283950617283950617 | |||
Wholesale Trade | 868073.0 | 10716.95061728395061728395061728395 | |||
Arts and Recreation Services | 156984.0 | 1938.074074074074074074074074074074 | |||
2020 | 22770336.0 | 14709.51937984496124031007751937984 | |||
Rental, Hiring and Real Estate Services | 1465873.0 | 18097.19753086419753086419753086420 | |||
Mining | 92843.0 | 1146.209876543209876543209876543210 | |||
Other Services | 298262.0 | 3682.246913580246913580246913580247 | |||
Administrative and Support Services | 404787.0 | 4997.370370370370370370370370370370 | |||
Health Care and Social Assistance | 578302.0 | 7139.530864197530864197530864197531 | |||
Manufacturing | 1540334.0 | 19016.46913580246913580246913580247 | |||
Transport, Postal and Warehousing | 579407.0 | 7153.172839506172839506172839506173 | |||
Agriculture, Forestry and Fishing | 1230707.0 | 15193.91358024691358024691358024691 | |||
Electricity, Gas, Water and Waste Services | 442509.0 | 5463.074074074074074074074074074074 | |||
Retail Trade | 1081675.0 | 13354.01234567901234567901234567901 | |||
Public Administration and Safety | 50871.0 | 628.0370370370370370370370370370370 | |||
Financial and Insurance Services | 3187409.0 | 39350.72839506172839506172839506173 | |||
Accommodation and Food Services | 553217.0 | 6829.839506172839506172839506172840 | |||
Information Media and Telecommunications | 224084.0 | 2766.469135802469135802469135802469 | |||
Professional, Scientific and Technical Services | 915891.0 | 11307.29629629629629629629629629630 | |||
All Industries | 7607349.0 | 845261.0 | |||
Education and Training | 171708.0 | 2119.851851851851851851851851851852 | |||
Construction | 1071439.0 | 13227.64197530864197530864197530864 | |||
Wholesale Trade | 1092697.0 | 13490.08641975308641975308641975309 | |||
Arts and Recreation Services | 180972.0 | 2234.222222222222222222222222222222 | |||
2017 | 19971573.0 | 12901.53294573643410852713178294574 | |||
Rental, Hiring and Real Estate Services | 1195456.0 | 14758.71604938271604938271604938272 | |||
Mining | 81828.0 | 1010.222222222222222222222222222222 | |||
Other Services | 264072.0 | 3260.148148148148148148148148148148 | |||
Administrative and Support Services | 350933.0 | 4332.506172839506172839506172839506 | |||
Health Care and Social Assistance | 518648.0 | 6403.061728395061728395061728395062 | |||
Manufacturing | 1375371.0 | 16979.88888888888888888888888888889 | |||
Transport, Postal and Warehousing | 507208.0 | 6261.827160493827160493827160493827 | |||
Agriculture, Forestry and Fishing | 1120211.0 | 13829.76543209876543209876543209877 | |||
Electricity, Gas, Water and Waste Services | 365535.0 | 4512.777777777777777777777777777778 | |||
Retail Trade | 979185.0 | 12088.70370370370370370370370370370 | |||
Public Administration and Safety | 45004.0 | 555.6049382716049382716049382716049 | |||
Financial and Insurance Services | 2825184.0 | 34878.81481481481481481481481481481 | |||
Accommodation and Food Services | 493103.0 | 6087.691358024691358024691358024691 | |||
Information Media and Telecommunications | 206974.0 | 2555.234567901234567901234567901235 | |||
Professional, Scientific and Technical Services | 784626.0 | 9686.740740740740740740740740740741 | |||
All Industries | 6689013.0 | 743223.6666666666666666666666666667 | |||
Education and Training | 156564.0 | 1932.888888888888888888888888888889 | |||
Construction | 876201.0 | 10817.29629629629629629629629629630 | |||
Wholesale Trade | 962118.0 | 11878.0 | |||
Arts and Recreation Services | 174339.0 | 2152.333333333333333333333333333333 | |||
2011 | 16606991.0 | 10728.03036175710594315245478036176 | |||
Rental, Hiring and Real Estate Services | 875506.0 | 10808.71604938271604938271604938272 | |||
Mining | 117128.0 | 1446.024691358024691358024691358025 | |||
Other Services | 219366.0 | 2708.222222222222222222222222222222 | |||
Administrative and Support Services | 276281.0 | 3410.876543209876543209876543209877 | |||
Health Care and Social Assistance | 433505.0 | 5351.913580246913580246913580246914 | |||
Manufacturing | 1274588.0 | 15735.65432098765432098765432098765 | |||
Transport, Postal and Warehousing | 448624.0 | 5538.567901234567901234567901234568 | |||
Agriculture, Forestry and Fishing | 956289.0 | 11806.03703703703703703703703703704 | |||
Electricity, Gas, Water and Waste Services | 342149.0 | 4224.061728395061728395061728395062 | |||
Retail Trade | 823416.0 | 10165.62962962962962962962962962963 | |||
Public Administration and Safety | 27670.0 | 341.6049382716049382716049382716049 | |||
Financial and Insurance Services | 2411805.0 | 29775.37037037037037037037037037037 | |||
Accommodation and Food Services | 371342.0 | 4584.469135802469135802469135802469 | |||
Information Media and Telecommunications | 221778.0 | 2738.0 | |||
Professional, Scientific and Technical Services | 620627.0 | 7662.061728395061728395061728395062 | |||
All Industries | 5543169.0 | 615907.6666666666666666666666666667 | |||
Education and Training | 126562.0 | 1562.493827160493827160493827160494 | |||
Construction | 588202.0 | 7261.753086419753086419753086419753 | |||
Wholesale Trade | 787761.0 | 9725.444444444444444444444444444444 | |||
Arts and Recreation Services | 141223.0 | 1743.493827160493827160493827160494 | |||
2015 | 18489341.0 | 11944.01873385012919896640826873385 | |||
Rental, Hiring and Real Estate Services | 1052271.0 | 12991.0 | |||
Mining | 113810.0 | 1405.061728395061728395061728395062 | |||
Other Services | 241886.0 | 2986.246913580246913580246913580247 | |||
Administrative and Support Services | 315654.0 | 3896.962962962962962962962962962963 | |||
Health Care and Social Assistance | 484024.0 | 5975.604938271604938271604938271605 | |||
Manufacturing | 1333676.0 | 16465.13580246913580246913580246914 | |||
Transport, Postal and Warehousing | 477372.0 | 5893.481481481481481481481481481481 | |||
Agriculture, Forestry and Fishing | 1057814.0 | 13059.43209876543209876543209876543 | |||
Electricity, Gas, Water and Waste Services | 374604.0 | 4624.740740740740740740740740740741 | |||
Retail Trade | 902358.0 | 11140.22222222222222222222222222222 | |||
Public Administration and Safety | 36404.0 | 449.4320987654320987654320987654321 | |||
Financial and Insurance Services | 2614941.0 | 32283.22222222222222222222222222222 | |||
Accommodation and Food Services | 415225.0 | 5126.234567901234567901234567901235 | |||
Information Media and Telecommunications | 223511.0 | 2759.395061728395061728395061728395 | |||
Professional, Scientific and Technical Services | 711419.0 | 8782.950617283950617283950617283951 | |||
All Industries | 6185603.0 | 687289.2222222222222222222222222222 | |||
Education and Training | 142863.0 | 1763.740740740740740740740740740741 | |||
Construction | 747389.0 | 9227.024691358024691358024691358025 | |||
Wholesale Trade | 894847.0 | 11047.49382716049382716049382716049 | |||
Arts and Recreation Services | 163670.0 | 2020.617283950617283950617283950617 | |||
2016 | 19124775.0 | 12354.50581395348837209302325581395 | |||
Rental, Hiring and Real Estate Services | 1121242.0 | 13842.49382716049382716049382716049 | |||
Mining | 91742.0 | 1132.617283950617283950617283950617 | |||
Other Services | 249918.0 | 3085.407407407407407407407407407407 | |||
Administrative and Support Services | 331248.0 | 4089.481481481481481481481481481481 | |||
Health Care and Social Assistance | 496094.0 | 6124.617283950617283950617283950617 | |||
Manufacturing | 1337211.0 | 16508.77777777777777777777777777778 | |||
Transport, Postal and Warehousing | 495140.0 | 6112.839506172839506172839506172840 | |||
Agriculture, Forestry and Fishing | 1073602.0 | 13254.34567901234567901234567901235 | |||
Electricity, Gas, Water and Waste Services | 360285.0 | 4447.962962962962962962962962962963 | |||
Retail Trade | 932624.0 | 11513.87654320987654320987654320988 | |||
Public Administration and Safety | 42900.0 | 529.6296296296296296296296296296296 | |||
Financial and Insurance Services | 2772232.0 | 34225.08641975308641975308641975309 | |||
Accommodation and Food Services | 441040.0 | 5444.938271604938271604938271604938 | |||
Information Media and Telecommunications | 194420.0 | 2400.246913580246913580246913580247 | |||
Professional, Scientific and Technical Services | 736316.0 | 9090.320987654320987654320987654321 | |||
All Industries | 6414442.0 | 712715.7777777777777777777777777778 | |||
Education and Training | 151203.0 | 1866.703703703703703703703703703704 | |||
Construction | 798814.0 | 9861.901234567901234567901234567901 | |||
Wholesale Trade | 913542.0 | 11278.29629629629629629629629629630 | |||
Arts and Recreation Services | 170760.0 | 2108.148148148148148148148148148148 | |||
2012 | 16950881.0 | 10950.18152454780361757105943152455 | |||
Rental, Hiring and Real Estate Services | 905334.0 | 11176.96296296296296296296296296296 | |||
Mining | 115803.0 | 1429.666666666666666666666666666667 | |||
Other Services | 223915.0 | 2764.382716049382716049382716049383 | |||
Administrative and Support Services | 286350.0 | 3535.185185185185185185185185185185 | |||
Health Care and Social Assistance | 440834.0 | 5442.395061728395061728395061728395 | |||
Manufacturing | 1285851.0 | 15874.70370370370370370370370370370 | |||
Transport, Postal and Warehousing | 431622.0 | 5328.666666666666666666666666666667 | |||
Agriculture, Forestry and Fishing | 981298.0 | 12114.79012345679012345679012345679 | |||
Electricity, Gas, Water and Waste Services | 357895.0 | 4418.456790123456790123456790123457 | |||
Retail Trade | 837105.0 | 10334.62962962962962962962962962963 | |||
Public Administration and Safety | 31041.0 | 383.2222222222222222222222222222222 | |||
Financial and Insurance Services | 2455656.0 | 30316.74074074074074074074074074074 | |||
Accommodation and Food Services | 385758.0 | 4762.444444444444444444444444444444 | |||
Information Media and Telecommunications | 223639.0 | 2760.975308641975308641975308641975 | |||
Professional, Scientific and Technical Services | 631644.0 | 7798.074074074074074074074074074074 | |||
All Industries | 5664227.0 | 629358.5555555555555555555555555556 | |||
Education and Training | 127219.0 | 1570.604938271604938271604938271605 | |||
Construction | 604158.0 | 7458.740740740740740740740740740741 | |||
Wholesale Trade | 807250.0 | 9966.049382716049382716049382716049 | |||
Arts and Recreation Services | 154282.0 | 1904.716049382716049382716049382716 | |||
2013 | 16984331.0 | 10971.79005167958656330749354005168 | |||
Rental, Hiring and Real Estate Services | 938279.0 | 11583.69135802469135802469135802469 | |||
Mining | 123839.0 | 1528.876543209876543209876543209877 | |||
Other Services | 224661.0 | 2773.592592592592592592592592592593 | |||
Administrative and Support Services | 292889.0 | 3615.913580246913580246913580246914 | |||
Health Care and Social Assistance | 447094.0 | 5519.679012345679012345679012345679 | |||
Manufacturing | 1269578.0 | 15673.80246913580246913580246913580 | |||
Transport, Postal and Warehousing | 435694.0 | 5378.938271604938271604938271604938 | |||
Agriculture, Forestry and Fishing | 977816.0 | 12071.80246913580246913580246913580 | |||
Electricity, Gas, Water and Waste Services | 342338.0 | 4226.395061728395061728395061728395 | |||
Retail Trade | 850872.0 | 10504.59259259259259259259259259259 | |||
Public Administration and Safety | 34749.0 | 429.0 | |||
Financial and Insurance Services | 2364424.0 | 29190.41975308641975308641975308642 | |||
Accommodation and Food Services | 393673.0 | 4860.160493827160493827160493827160 | |||
Information Media and Telecommunications | 214417.0 | 2647.123456790123456790123456790123 | |||
Professional, Scientific and Technical Services | 650865.0 | 8035.370370370370370370370370370370 | |||
All Industries | 5680357.0 | 631150.7777777777777777777777777778 | |||
Education and Training | 126114.0 | 1556.962962962962962962962962962963 | |||
Construction | 635817.0 | 7849.592592592592592592592592592593 | |||
Wholesale Trade | 825011.0 | 10185.32098765432098765432098765432 | |||
Arts and Recreation Services | 155844.0 | 1924.0 | |||
2019 | 22040794.0 | 14238.23901808785529715762273901809 | |||
Rental, Hiring and Real Estate Services | 1374826.0 | 16973.16049382716049382716049382716 | |||
Mining | 91471.0 | 1129.271604938271604938271604938272 | |||
Other Services | 287599.0 | 3550.604938271604938271604938271605 | |||
Administrative and Support Services | 394332.0 | 4868.296296296296296296296296296296 | |||
Health Care and Social Assistance | 558627.0 | 6896.629629629629629629629629629630 | |||
Manufacturing | 1500013.0 | 18518.67901234567901234567901234568 | |||
Transport, Postal and Warehousing | 572059.0 | 7062.456790123456790123456790123457 | |||
Agriculture, Forestry and Fishing | 1206071.0 | 14889.76543209876543209876543209877 | |||
Electricity, Gas, Water and Waste Services | 412243.0 | 5089.419753086419753086419753086420 | |||
Retail Trade | 1047128.0 | 12927.50617283950617283950617283951 | |||
Public Administration and Safety | 45620.0 | 563.2098765432098765432098765432099 | |||
Financial and Insurance Services | 3119342.0 | 38510.39506172839506172839506172840 | |||
Accommodation and Food Services | 540263.0 | 6669.913580246913580246913580246914 | |||
Information Media and Telecommunications | 216052.0 | 2667.308641975308641975308641975309 | |||
Professional, Scientific and Technical Services | 875036.0 | 10802.91358024691358024691358024691 | |||
All Industries | 7366333.0 | 818481.4444444444444444444444444444 | |||
Education and Training | 170110.0 | 2100.123456790123456790123456790123 | |||
Construction | 1024114.0 | 12643.38271604938271604938271604938 | |||
Wholesale Trade | 1067885.0 | 13183.76543209876543209876543209877 | |||
Arts and Recreation Services | 171670.0 | 2119.382716049382716049382716049383 | |||
2018 | 20914247.0 | 13510.49547803617571059431524547804 | |||
Rental, Hiring and Real Estate Services | 1268035.0 | 15654.75308641975308641975308641975 | |||
Mining | 82182.0 | 1014.592592592592592592592592592593 | |||
Other Services | 276024.0 | 3407.703703703703703703703703703704 | |||
Administrative and Support Services | 380718.0 | 4700.222222222222222222222222222222 | |||
Health Care and Social Assistance | 529891.0 | 6541.864197530864197530864197530864 | |||
Manufacturing | 1443526.0 | 17821.30864197530864197530864197531 | |||
Transport, Postal and Warehousing | 542080.0 | 6692.345679012345679012345679012346 | |||
Agriculture, Forestry and Fishing | 1161882.0 | 14344.22222222222222222222222222222 | |||
Electricity, Gas, Water and Waste Services | 388419.0 | 4795.296296296296296296296296296296 | |||
Retail Trade | 1017321.0 | 12559.51851851851851851851851851852 | |||
Public Administration and Safety | 46008.0 | 568.0 | |||
Financial and Insurance Services | 2930553.0 | 36179.66666666666666666666666666667 | |||
Accommodation and Food Services | 514228.0 | 6348.493827160493827160493827160494 | |||
Information Media and Telecommunications | 215095.0 | 2655.493827160493827160493827160494 | |||
Professional, Scientific and Technical Services | 819811.0 | 10121.12345679012345679012345679012 | |||
All Industries | 6986319.0 | 776257.6666666666666666666666666667 | |||
Education and Training | 161120.0 | 1989.135802469135802469135802469136 | |||
Construction | 956983.0 | 11814.60493827160493827160493827160 | |||
Wholesale Trade | 1016941.0 | 12554.82716049382716049382716049383 | |||
Arts and Recreation Services | 177111.0 | 2186.555555555555555555555555555556 |