# Copyright 2025 qBraid
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""
Display information about quantum jobs in table view.
"""
from typing import Optional
try:
from IPython.display import HTML, clear_output, display
except ImportError: # pragma: no cover
pass
from qbraid._display import running_in_jupyter
def _job_table_basic(data: list[str], message: str) -> None:
if len(data) == 0:
print(message)
else:
headers = ["Job ID", "Submitted", "Status"]
max_job_id_length = max(len(str(job_id)) for job_id, _, _ in data)
max_job_id_length = max(max_job_id_length, len(headers[0]))
header_format = "{:<" + str(max_job_id_length) + "} {:<25} {:<15}"
separator_format = "{:<" + str(max_job_id_length) + "} {:<25} {:<15}"
row_format = "{:<" + str(max_job_id_length) + "} {:<25} {:<15}"
print(f"{message}:\n")
print(header_format.format(*headers))
separators = ["-" * len(header) for header in headers]
print(separator_format.format(*separators))
for job_id, timestamp, status in data:
print(row_format.format(job_id, timestamp, status))
def _job_table_jupyter(data: list[str], message: Optional[str] = None, align: str = "right"):
clear_output(wait=True)
html = """<h3>Quantum Jobs</h3><table><tr>
<th style='text-align:left'>qBraid ID</th>
<th style='text-align:left'>Submitted</th>
<th style='text-align:left'>Status</th></tr>
"""
for job_id, timestamp, status_str in data:
if status_str == "COMPLETED":
color = "green"
elif status_str == "FAILED":
color = "red"
elif status_str in [
"INITIALIZING",
"INITIALIZED",
"CREATED",
"QUEUED",
"VALIDATING",
"RUNNING",
]:
color = "blue"
else:
color = "grey"
status = f"<span style='color:{color}'>{status_str}</span>"
html += f"""<tr>
<td style='text-align:left'>{job_id}</td>
<td style='text-align:left'>{timestamp}</td>
<td style='text-align:left'>{status}</td></tr>
"""
html += f"<tr><td colspan='4'; style='text-align:{align}'>{message}</td></tr>"
html += "</table>"
return display(HTML(html))
[docs]
def display_jobs_from_data(job_data: list[list[str]], message: Optional[str] = None):
"""Displays a list of quantum jobs submitted by user, tabulated by job ID,
the date/time it was submitted, and status."""
align = "center" if len(job_data) == 0 else "right"
if not message:
num_jobs = len(job_data)
if num_jobs == 0:
message = "No jobs found matching criteria."
else:
message = f"Displaying {num_jobs} job{'s' if num_jobs > 1 else ''}."
if running_in_jupyter():
return _job_table_jupyter(job_data, message, align=align)
return _job_table_basic(job_data, message)