-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from climateintelligence/add-report-process
Add report process
- Loading branch information
Showing
12 changed files
with
290 additions
and
81 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,7 +6,6 @@ | |
|
||
installation | ||
configuration | ||
notebooks/index | ||
dev_guide | ||
processes | ||
authors | ||
|
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
from rdflib import Graph, URIRef | ||
from rdflib.plugins.sparql import prepareQuery | ||
|
||
from pywps import configuration | ||
|
||
# Provide the path to the SQLite database in the local folder | ||
# DB_URL = configuration.get_config_value("provenance", "db_url") | ||
DB_URL = configuration.get_config_value("logging", "database") | ||
|
||
|
||
class GraphDB(object): | ||
def __init__(self): | ||
# Create a graph with a specific backend store | ||
self.graph = Graph( | ||
store="SQLAlchemy", identifier=URIRef("http://example.org/graph") | ||
) | ||
self.graph.open(DB_URL, create=True) | ||
|
||
def add(self, data): | ||
new_graph = Graph() | ||
new_graph.parse(data=data, format="turtle") | ||
|
||
# add rdf to existing graph | ||
for triple in new_graph: | ||
self.graph.add(triple) | ||
# Commit changes to the store | ||
self.graph.commit() | ||
|
||
def query(self, query_str): | ||
namespaces = { | ||
"rdf": "http://www.w3.org/1999/02/22-rdf-syntax-ns#", | ||
"foaf": "http://xmlns.com/foaf/0.1/", | ||
"rdfs": "http://www.w3.org/2000/01/rdf-schema#", | ||
"prov": "http://www.w3.org/ns/prov#", | ||
"provone": "http://purl.dataone.org/provone/2015/01/15/ontology#", | ||
"dcterms": "http://purl.org/dc/terms/", | ||
"clint": "urn:clint:", | ||
} | ||
query = prepareQuery(query_str, initNs=namespaces) | ||
results = self.graph.query(query) | ||
return results |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
from .wps_say_hello import SayHello | ||
from .wps_dashboard import Dashboard | ||
|
||
processes = [ | ||
SayHello(), | ||
Dashboard(), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
from pathlib import Path | ||
|
||
from pywps import Process, LiteralInput, ComplexOutput, Format | ||
|
||
from parrot import query | ||
|
||
|
||
class Dashboard(Process): | ||
def __init__(self): | ||
inputs = [ | ||
LiteralInput( | ||
"time", | ||
"Time Period", | ||
abstract="The time period for the report seperated by /" | ||
"Example: 2023-09-01/2023-09-30", | ||
data_type="string", | ||
default="2023-09-01/2023-09-30", | ||
min_occurs=0, | ||
max_occurs=1, | ||
), | ||
] | ||
outputs = [ | ||
ComplexOutput( | ||
"report", | ||
"Generated HTML Report", | ||
as_reference=True, | ||
supported_formats=[Format("text/html")], | ||
), | ||
] | ||
|
||
super(Dashboard, self).__init__( | ||
self._handler, | ||
identifier="dashboard", | ||
title="Generate HTML Report", | ||
version="1.0", | ||
abstract="Generate an HTML report from a provenance database.", | ||
inputs=inputs, | ||
outputs=outputs, | ||
status_supported=True, | ||
store_supported=True, | ||
) | ||
|
||
def _handler(self, request, response): | ||
workdir = Path(self.workdir) | ||
# input_csv = request.inputs['input_csv'][0].file | ||
|
||
# Query the provenance database ... result is a Pandas DataFrame | ||
df = query.query() | ||
|
||
# Generate an HTML report from the DataFrame | ||
html_report = self.write_html(df, workdir) | ||
|
||
print(f"report: {html_report}") | ||
response.outputs["report"].file = html_report | ||
# response.outputs["report"].output_format = Format("text/html") | ||
|
||
return response | ||
|
||
def write_html(self, df, workdir): | ||
# Convert the DataFrame to an HTML table | ||
html_table = df.to_html(escape=False, index=False) | ||
|
||
# Define the HTML template | ||
html_template = f""" | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<title>Provenance Report</title> | ||
<style> | ||
table {{ | ||
border-collapse: collapse; | ||
width: 100%; | ||
border: 1px solid #ddd; | ||
}} | ||
th, td {{ | ||
text-align: left; | ||
padding: 8px; | ||
}} | ||
th {{ | ||
background-color: #f2f2f2; | ||
}} | ||
tr:nth-child(even) {{ | ||
background-color: #f2f2f2; | ||
}} | ||
</style> | ||
</head> | ||
<body> | ||
<h1>Provenance Report</h1> | ||
{html_table} | ||
</body> | ||
</html> | ||
""" | ||
|
||
# Write the HTML template to a file | ||
outfile = workdir / "provenance_report.html" | ||
with outfile.open(mode="w") as file: | ||
file.write(html_template) | ||
|
||
return outfile |
Oops, something went wrong.