Skip to content

Commit 227e554

Browse files
committed
Merge branch 'refactor_utils' into 'main'
Refactor utils See merge request cschmidt/4c-webviewer!3
2 parents a2bb9ab + 5c56009 commit 227e554

File tree

6 files changed

+120
-35
lines changed

6 files changed

+120
-35
lines changed

src/fourc_webviewer/cli_utils.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,22 @@
1+
"""CLI utils module."""
2+
13
import argparse
24

35
import fourc_webviewer.run_webserver as webserver
46

57

68
def main():
9+
"""Get the CLI arguments and start the webviewer."""
710
arguments = get_arguments()
811
webserver.run_webviewer(**arguments)
912

1013

1114
def get_arguments():
15+
"""Get the CLI arguments.
16+
17+
Returns:
18+
dict: Arguments dictionary
19+
"""
1220
parser = argparse.ArgumentParser(description="4C Webviewer")
1321
parser.add_argument("--dat_file", type=str, help="input file path to visualize")
1422

src/fourc_webviewer/input_file_utils/dat_file_visualization.py

Lines changed: 34 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
"""Dat file visualization."""
2+
13
import lnmmeshio
24
import numpy as np
35
import os
@@ -12,12 +14,15 @@
1214

1315

1416
def convert_to_vtu(dat_file_path, temp_dir):
15-
# This function converts a given dat file into a corresponding vtu file
16-
# Input:
17-
# dat_file_path: string: full path to the dat file to be converted
18-
# Output:
19-
# vtu_file_path: full path of the converted file
17+
"""Convert dat file to vtu.
18+
19+
Args:
20+
dat_file_path (str, Path): Path to dat file
21+
temp_dir (str, Path): Temp directory
2022
23+
Returns:
24+
str: Path to vtu file
25+
"""
2126
# define the vtu_file_path to have the same name as the dat file, but the its directory is in './temp_files'
2227
vtu_file_path = str(
2328
Path(temp_dir) / f"{os.path.splitext(os.path.basename(dat_file_path))[0]}.vtu"
@@ -36,6 +41,14 @@ def convert_to_vtu(dat_file_path, temp_dir):
3641

3742

3843
def function_plot_figure(state_data):
44+
"""Get function plot figure.
45+
46+
Args:
47+
state_data (trame_server.core.Server): Trame server state
48+
49+
Returns:
50+
plotly.graph_objects._figure.Figure: Figure to be plotted
51+
"""
3952
num_of_time_points = 1000 # number of discrete time points used for plotting
4053
data = {
4154
"t": np.linspace(0, state_data.MAX_TIME, num_of_time_points),
@@ -66,11 +79,14 @@ def function_plot_figure(state_data):
6679

6780

6881
def return_function_from_funct_string(funct_string):
69-
# The function takes in the value of the funct string coming from a function and returns the parsed function
70-
# Input:
71-
# funct_string: string of the function, containing the variables t, x, y, z
72-
# Output:
73-
# return_funct: function of the mentioned arguments
82+
"""Create function from funct string.
83+
84+
Args:
85+
funct_string (str): Funct definition
86+
87+
Returns:
88+
callable: callable function of x, y, z, t
89+
"""
7490

7591
def funct_using_eval(x, y, z, t):
7692
# defined functions to be replaced: <def_funct> becomes <np.funct>
@@ -101,7 +117,14 @@ def funct_using_eval(x, y, z, t):
101117
return np.frompyfunc(funct_using_eval, 4, 1)
102118

103119

104-
def to_vtu(dis, vtu_file: str, override=True, ascii=False):
120+
def to_vtu(dis, vtu_file: str, override=True):
121+
"""Discretization to vtu.
122+
123+
Args:
124+
dis (lnmmeshio.Discretization): Discretization object
125+
vtu_file (str): Path to vtu file
126+
override (bool, optional): Overwrite existing file. Defaults to True
127+
"""
105128
add_dat_file_data_to_dis(dis)
106129

107130
# write case file
@@ -111,12 +134,3 @@ def to_vtu(dis, vtu_file: str, override=True, ascii=False):
111134
file_format="vtu",
112135
override=override,
113136
)
114-
115-
116-
def validate_vtu_file_path(vtu_file_path):
117-
# Validate the file path: file has to exist and end with ".vtu"
118-
if not vtu_file_path.endswith(".dat"):
119-
raise Exception("Provided file does not end with .dat!")
120-
121-
if not os.path.exists(vtu_file_path):
122-
raise Exception("Provided file does not exist!")

src/fourc_webviewer/input_file_utils/read_dat_file.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
# ------------------------------------------------------------------------------#
2-
# READ AND SECTION A PROVIDED DAT FILE #
3-
# ------------------------------------------------------------------------------#
4-
import os
1+
"""Dat file reader utils."""
2+
53
import re
64

75
from fourc_webviewer.python_utils import list_is_iterable

src/fourc_webviewer/input_file_utils/write_dat_file.py

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
# ------------------------------------------------------------------------------#
2-
# WRITE A DAT FILE FROM INPUT INFO #
3-
# ------------------------------------------------------------------------------#
1+
"""Dat file writer utils."""
42

53
import math
64
import re
@@ -214,8 +212,3 @@ def flatten_input_list(lst):
214212
else:
215213
flattened_list.append(item)
216214
return flattened_list
217-
218-
219-
if __name__ == "__main__":
220-
...
221-
# no need to implement anything here, as the method can only be called from within the main.py
Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,17 @@
1+
""" Module for python utils."""
2+
3+
14
def list_is_iterable(obj):
5+
"""Check if object is iterable.
6+
7+
Args:
8+
obj (obj): Object to be checked
9+
10+
Returns:
11+
bool: True if object is iterable
12+
"""
213
try:
314
iter(obj)
4-
except Exception:
15+
except TypeError:
516
return False
6-
else:
7-
return True
17+
return True

test/test_pyvista.py

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import tempfile
2+
from pathlib import Path
3+
4+
import pyvista as pv
5+
from pyvista.trame.ui import plotter_ui
6+
from trame.app import get_server
7+
from trame.app.file_upload import ClientFile
8+
from trame.ui.vuetify3 import SinglePageLayout
9+
from trame.widgets import vuetify3
10+
11+
pv.OFF_SCREEN = True
12+
13+
server = get_server()
14+
state, ctrl = server.state, server.controller
15+
16+
pl = pv.Plotter()
17+
18+
19+
@server.state.change("file_exchange")
20+
def handle(file_exchange, **kwargs) -> None:
21+
if file_exchange:
22+
23+
file = ClientFile(file_exchange[0])
24+
if file.content:
25+
pl.remove_actor("mesh")
26+
bytes = file.content # noqa: A001
27+
with tempfile.NamedTemporaryFile(suffix=file.name) as path:
28+
with Path(path.name).open("wb") as f:
29+
f.write(bytes)
30+
ds = pv.read(path.name).extract_surface()
31+
pl.add_mesh(ds, name="mesh")
32+
pl.reset_camera()
33+
else:
34+
pl.clear_actors()
35+
pl.reset_camera()
36+
37+
38+
with SinglePageLayout(server) as layout:
39+
with layout.toolbar:
40+
vuetify3.VSpacer()
41+
vuetify3.VFileInput(
42+
multiple=False,
43+
show_size=True,
44+
small_chips=True,
45+
truncate_length=25,
46+
v_model=("file_exchange", None),
47+
density="compact",
48+
hide_details=True,
49+
style="max-width: 300px;",
50+
)
51+
vuetify3.VProgressLinear(
52+
indeterminate=True, absolute=True, bottom=True, active=("trame__busy",)
53+
)
54+
55+
with layout.content: # noqa: SIM117
56+
with vuetify3.VContainer(
57+
fluid=True, classes="pa-0 fill-height", style="position: relative;"
58+
):
59+
view = plotter_ui(pl)
60+
ctrl.view_update = view.update
61+
# Show UI
62+
server.start()

0 commit comments

Comments
 (0)