Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

argument plot_types added #1594

Merged
merged 3 commits into from
Nov 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
191 changes: 101 additions & 90 deletions mslib/mswms/gallery_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
limitations under the License.
"""

# ToDo refactor to use Jinja2 templates

import os
from PIL import Image
import io
Expand Down Expand Up @@ -73,12 +75,12 @@
<head>
<style>

.gtooltip {
.gtooltip {{
position: relative;
display: inline-block;
}
}}

.gallery .gtooltiptext {
.gallery .gtooltiptext {{
visibility: hidden;
width: 400px;
background-color: lavender;
Expand All @@ -91,9 +93,9 @@
top: 150%;
left: 50%;
margin-left: -200px;
}
}}

.gallery .gtooltiptext::after {
.gallery .gtooltiptext::after {{
content: "";
position: absolute;
bottom: 100%;
Expand All @@ -102,21 +104,21 @@
border-width: 5px;
border-style: solid;
border-color: transparent transparent black transparent;
}
}}

.gallery:hover .gtooltiptext {
.gallery:hover .gtooltiptext {{
visibility: visible;
}
}}

/* Style the tab */
.tab {
.tab {{
overflow: hidden;
border: 1px solid #DDDDFF;
background-color: #E6E6FA;
}
}}

/* Style the buttons inside the tab */
.tab button {
.tab button {{
background-color: inherit;
float: left;
border: none;
Expand All @@ -125,82 +127,82 @@
padding: 14px 16px;
transition: 0.3s;
font-size: 17px;
}
}}

/* Change background color of buttons on hover */
.tab button:hover {
.tab button:hover {{
background-color: #DDDDFF;
}
}}

/* Create an active/current tablink class */
.tab button.active {
.tab button.active {{
background-color: #CCCCFF;
}
}}

.tab input[type=text]{
.tab input[type=text]{{
float: right;
padding: 6px;
border: 2px solid #DDDDFF;
margin-top: 8px;
margin-right: 16px;
font-size: 17px;
}
}}

.tab select{
.tab select{{
float: right;
padding: 6px;
border: 2px solid #DDDDFF;
background-color: #F7F7F7;
margin-top: 8px;
margin-right: 16px;
font-size: 17px;
}
}}

/* Style the tab content */
.tabcontent {
.tabcontent {{
display: none;
padding: 6px 12px;
-webkit-animation: fadeEffect 1s;
animation: fadeEffect 1s;
}
}}

.tabcontent::after {
.tabcontent::after {{
content: "";
clear: both;
display: block;
float: none;
}
}}

/* Style the plots content */
div.gallery {
div.gallery {{
text-align: center;
margin: 0.5%;
border: 1px solid #ccc;
float: left;
width: 24%;
box-shadow: 0 0 2px 1px rgba(0, 0, 0, 0.2);
transition: box-shadow 0.3s ease-in-out;
}
}}

div.gallery:hover {
div.gallery:hover {{
box-shadow: 0 0 5px 1px rgba(0, 0, 255, 0.5);
}
}}

div.gallery img {
div.gallery img {{
width: 100%;
height: auto;
}
}}

/* Fade in tabs */
@-webkit-keyframes fadeEffect {
from {opacity: 0;}
to {opacity: 1;}
}

@keyframes fadeEffect {
from {opacity: 0;}
to {opacity: 1;}
}
@-webkit-keyframes fadeEffect {{
from {{opacity: 0;}}
to {{opacity: 1;}}
}}

@keyframes fadeEffect {{
from {{opacity: 0;}}
to {{opacity: 1;}}
}}
</style>
</head>
<body onload="hideEmptySelects();
Expand All @@ -214,9 +216,7 @@
<h3>Plot Gallery</h3>

<div class="tab">
<button class="tablinks active" onclick="openTab(event, 'Top-View')">Top Views</button>
<button class="tablinks" onclick="openTab(event, 'Side-View')">Side Views</button>
<button class="tablinks" onclick="openTab(event, 'Linear-View')">Linear Views</button>
{section}
<input type="text" placeholder="Search..." id="gallery-filter" oninput="filterContent()"></input>
<select name="levels" id="level-select" onchange="changeImages()"></select>
<select name="times" id="time-select" onchange="changeImages()"></select>
Expand Down Expand Up @@ -431,21 +431,29 @@ def write_code_pages(path, sphinx=False, url_prefix=None):
f.write(f""".. raw:: html\n\n{plot_htmls[layer]}""")


def write_html(path, sphinx=False):
def write_html(path, sphinx=False, plot_types=None):
if plot_types is None:
plot_types = plots.keys()
"""
Writes the plots.html file containing the gallery
"""
html = begin
section = []
for name in plot_types:
section.append(f""""<button class="tablinks active" """
f"""onclick="openTab(event, "{name}-View')">{name} Views</button>""")
section = ".\n".join(section)
html = begin.format(section=section)
if sphinx:
html = html.replace("<h3>Plot Gallery</h3>", "")

for l_type in plots:
style = ""
if l_type == "Top":
style = "style=\"display: block;\""
html += f"<div id=\"{l_type}-View\" class=\"tabcontent\" {style}>"
html += "\n".join(plots[l_type])
html += "</div>"
if l_type in plot_types:
style = ""
if l_type == "Top":
style = "style=\"display: block;\""
html += f"<div id=\"{l_type}-View\" class=\"tabcontent\" {style}>"
html += "\n".join(plots[l_type])
html += "</div>"

with open(os.path.join(path, "plots.html"), "w+") as file:
file.write(html + end)
Expand Down Expand Up @@ -666,10 +674,13 @@ def create_linear_plot(xml, file_location):


def add_image(path, plot, plot_object, generate_code=False, sphinx=False, url_prefix="",
dataset=None, level=None, itime=None, vtime=None, simple_naming=False):
dataset=None, level=None, itime=None, vtime=None, simple_naming=False, plot_types=None):
"""
Adds the images to the plots folder and generates the html codes to display them
"""
if plot_types is None:
plot_types = plots.keys()

global end
# Import here due to some circular import issue if imported too soon
from mslib.index import SCRIPT_NAME
Expand All @@ -679,45 +690,45 @@ def add_image(path, plot, plot_object, generate_code=False, sphinx=False, url_pr

l_type = "Linear" if isinstance(plot_object, AbstractLinearSectionStyle) else \
"Side" if isinstance(plot_object, AbstractVerticalSectionStyle) else "Top"

filename = f"{l_type}_{dataset}{plot_object.name}-" + (
f"{level}it{itime}vt{vtime}".replace(" ", "_").replace(":", "_").replace("-", "_")
if not simple_naming else "")

if plot:
if not os.path.exists(os.path.join(path, "plots")):
os.mkdir(os.path.join(path, "plots"))
if l_type == "Linear":
create_linear_plot(etree.fromstring(plot), os.path.join(path, "plots", filename + ".png"))
else:
with Image.open(io.BytesIO(plot)) as image:
image.save(os.path.join(path, "plots", filename + ".png"),
format="PNG")

end = end.replace("files = [", f"files = [\"{filename}.png\",")\
.replace(",];", "];")
img_path = f"../_static/{filename}.png" if sphinx \
else f"{url_prefix}/static/plots/{filename}.png"
code_path = f"code/{l_type}_{dataset}{plot_object.name}.html" if sphinx \
else f"{url_prefix if url_prefix else ''}{SCRIPT_NAME}mss/code/{l_type}_{dataset}{plot_object.name}.md"

if generate_code:
if f"{l_type}_{dataset}{plot_object.name}" not in plot_htmls:
plot_htmls[f"{l_type}_{dataset}{plot_object.name}"] = \
plot_html_begin + get_plot_details(path, plot_object, l_type, sphinx, img_path, code_path, dataset)
markdown = plot_htmls[f"{l_type}_{dataset}{plot_object.name}"]
if level:
markdown = add_levels([level], None, markdown)
if vtime:
markdown = add_times(itime, [vtime], markdown)
plot_htmls[f"{l_type}_{dataset}{plot_object.name}"] = markdown

id = img_path.split("-" + f"{level}".replace(" ", "_").replace(":", "_").replace("-", "_"))[0]
if not any([id in html for html in plots[l_type]]):
plots[l_type].append(image_md(
img_path, plot_object.name, code_path if generate_code else None,
f"{plot_object.title}" + (f"<br>{plot_object.abstract}"
if plot_object.abstract else "")))
if l_type in plot_types:
filename = f"{l_type}_{dataset}{plot_object.name}-" + (
f"{level}it{itime}vt{vtime}".replace(" ", "_").replace(":", "_").replace("-", "_")
if not simple_naming else "")

if plot:
if not os.path.exists(os.path.join(path, "plots")):
os.mkdir(os.path.join(path, "plots"))
if l_type == "Linear":
create_linear_plot(etree.fromstring(plot), os.path.join(path, "plots", filename + ".png"))
else:
with Image.open(io.BytesIO(plot)) as image:
image.save(os.path.join(path, "plots", filename + ".png"),
format="PNG")

end = end.replace("files = [", f"files = [\"{filename}.png\",")\
.replace(",];", "];")
img_path = f"../_static/{filename}.png" if sphinx \
else f"{url_prefix}/static/plots/{filename}.png"
code_path = f"code/{l_type}_{dataset}{plot_object.name}.html" if sphinx \
else f"{url_prefix if url_prefix else ''}{SCRIPT_NAME}mss/code/{l_type}_{dataset}{plot_object.name}.md"

if generate_code:
if f"{l_type}_{dataset}{plot_object.name}" not in plot_htmls:
plot_htmls[f"{l_type}_{dataset}{plot_object.name}"] = \
plot_html_begin + get_plot_details(path, plot_object, l_type, sphinx, img_path, code_path, dataset)
markdown = plot_htmls[f"{l_type}_{dataset}{plot_object.name}"]
if level:
markdown = add_levels([level], None, markdown)
if vtime:
markdown = add_times(itime, [vtime], markdown)
plot_htmls[f"{l_type}_{dataset}{plot_object.name}"] = markdown

id = img_path.split("-" + f"{level}".replace(" ", "_").replace(":", "_").replace("-", "_"))[0]
if not any([id in html for html in plots[l_type]]):
plots[l_type].append(image_md(
img_path, plot_object.name, code_path if generate_code else None,
f"{plot_object.title}" + (f"<br>{plot_object.abstract}"
if plot_object.abstract else "")))


def add_levels(levels, l_type=None, text=None):
Expand Down
10 changes: 8 additions & 2 deletions mslib/mswms/mswms.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,15 @@ def main():
help="Normally the plot images should appear at the relative url /static/plots/*.png.\n"
"In case they are prefixed by something, e.g. /demo/static/plots/*.png,"
" please provide the prefix /demo here.")
gallery.add_argument("--plot_types", default=None,
help='A comma-separated list of all plot_types. \n'
'Default is ["Top", "Side", "Linear"]')

args = parser.parse_args()

if args.plot_types is None:
plot_types = ["Top", "Side", "Linear"]
else:
plot_types = [name.strip() for name in args.plot_types.split(',')]
if args.version:
print("***********************************************************************")
print("\n Mission Support System (MSS)\n")
Expand All @@ -102,7 +108,7 @@ def main():
create = args.create or args.refresh
clear = args.clear or args.refresh
server.generate_gallery(create, clear, args.show_code, url_prefix=args.url_prefix, levels=args.levels,
itimes=args.itimes, vtimes=args.vtimes)
itimes=args.itimes, vtimes=args.vtimes, plot_types=plot_types)
logging.info("Gallery generation done.")
sys.exit()

Expand Down
10 changes: 6 additions & 4 deletions mslib/mswms/wms.py
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,8 @@ def __init__(self):
self.register_lsec_layer(layer[1], layer_class=layer[0])

def generate_gallery(self, create=False, clear=False, generate_code=False, sphinx=False, plot_list=None,
all_plots=False, url_prefix="", levels="", itimes="", vtimes="", simple_naming=False):
all_plots=False, url_prefix="", levels="", itimes="", vtimes="", simple_naming=False,
plot_types=None):
"""
Iterates through all registered layers, draws their plots and puts them in the gallery
"""
Expand Down Expand Up @@ -393,7 +394,8 @@ def generate_gallery(self, create=False, clear=False, generate_code=False, sphin
generate_code, sphinx, url_prefix=url_prefix,
dataset=dataset if multiple_datasets else "", level=f"{level}" +
f"{vert_units}",
itime=str(itime), vtime=str(vtime), simple_naming=simple_naming)
itime=str(itime), vtime=str(vtime), simple_naming=simple_naming,
plot_types=plot_types)
if level:
add_levels([f"{level} {vert_units}"], vert_units)
else:
Expand All @@ -407,14 +409,14 @@ def generate_gallery(self, create=False, clear=False, generate_code=False, sphin
None if exists else plot_driver.plot(), plot_object, generate_code,
sphinx, url_prefix=url_prefix,
dataset=dataset if multiple_datasets else "", itime=str(itime),
vtime=str(vtime), simple_naming=simple_naming)
vtime=str(vtime), simple_naming=simple_naming, plot_types=plot_types)
add_times(itime, [vtime])

except Exception as e:
traceback.print_exc()
logging.error("%s %s %s", plot_object.name, type(e), e)

write_html(tmp_path, sphinx)
write_html(tmp_path, sphinx, plot_types=plot_types)
if generate_code:
write_code_pages(tmp_path, sphinx, url_prefix)
if sphinx:
Expand Down