diff --git a/README.md b/README.md index 0e31534..e39ef1c 100644 --- a/README.md +++ b/README.md @@ -209,7 +209,7 @@ Current configuration notes ## TODO - [x] HTTPS - via docker? -- [ ] Logging/analytics +- [x] Logging/analytics - [ ] `nginx` static files update - [ ] Simulation **g** vectors - [ ] Simulation table of most common zone axes diff --git a/blog.py b/blog.py index 7581d2c..a3eba76 100644 --- a/blog.py +++ b/blog.py @@ -20,7 +20,14 @@ logger.info("Log is working.") import matplotlib.pyplot as plt -from flask import Flask, Response, render_template, render_template_string, request, send_from_directory +from flask import ( + Flask, + Response, + render_template, + render_template_string, + request, + send_from_directory, +) from flask.helpers import redirect, url_for from flask_flatpages import FlatPages, pygments_style_defs from flask_flatpages.utils import pygmented_markdown @@ -41,7 +48,7 @@ load_dotenv() -from figs import create_dp_figure, create_structure_figure +from figs import create_dp_figure, create_structure_figure, get_mp_structure DEBUG = bool(os.environ.get("FLASK_DEBUG", False)) @@ -49,11 +56,7 @@ FLATPAGES_EXTENSION = ".md" FLATPAGES_ROOT = "content" FLATPAGES_MARKDOWN_EXTENSIONS = ["codehilite", "fenced_code"] -FLATPAGES_EXTENSION_CONFIGS = { - 'codehilite': { - 'linenums': 'True' - } -} +FLATPAGES_EXTENSION_CONFIGS = {"codehilite": {"linenums": "True"}} POST_DIR = "posts" @@ -102,6 +105,7 @@ def about(): def dp_sim(structure=None, zone_axis=None): logger.info(f"{request.remote_addr} - {request.full_path} - {request.referrer}") print(zone_axis) + message = None if request.args.get("structure") is not None and structure is None: try: structure = request.args.get("structure") @@ -122,20 +126,31 @@ def dp_sim(structure=None, zone_axis=None): else: zone_axis = [1, 1, 1] if structure is not None: - success = True + try: + get_mp_structure(structure=structure) + success = True + except Exception as e: + message = str(e) + success = False else: success = False h, k, l = zone_axis return render_template( - "dp_sim.html", success=success, structure=structure, h=h, k=k, l=l + "dp_sim.html", + success=success, + structure=structure, + h=h, + k=k, + l=l, + message=message, ) @app.route("/dp_sim/img/____structure_plot.png") def plot_structure_png(structure=None, h=None, k=None, l=None): - fname = f'assets/img/dps/{structure}_{h}_{k}_{l}_structure_plot.svg' - folder = 'static/' + fname = f"assets/img/dps/{structure}_{h}_{k}_{l}_structure_plot.svg" + folder = "static/" try: if os.path.isfile(folder + fname): with open(folder + fname) as f: @@ -156,8 +171,8 @@ def plot_structure_png(structure=None, h=None, k=None, l=None): @app.route("/dp_sim/img/____dp_plot.png") def plot_dp_png(structure=None, h=None, k=None, l=None): - fname = f'assets/img/dps/{structure}_{h}_{k}_{l}_dp_plot.svg' - folder = 'static/' + fname = f"assets/img/dps/{structure}_{h}_{k}_{l}_dp_plot.svg" + folder = "static/" try: if os.path.isfile(folder + fname): with open(folder + fname) as f: diff --git a/figs.py b/figs.py index 83de8fc..4d67057 100644 --- a/figs.py +++ b/figs.py @@ -12,28 +12,30 @@ # from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas from matplotlib.figure import Figure -MP_API_KEY = os.environ.get('MP_API_KEY') +MP_API_KEY = os.environ.get("MP_API_KEY") # surpress plt.show warnings because using non-interactive backend - can change py4DSTEM to not automatically plt.show things too? warnings.filterwarnings("ignore", category=UserWarning) -def get_mp_structure(structure='mp-81'): + +def get_mp_structure(structure="mp-81"): """ - This will try to get the Materials Project structure and save it, if it does not already exist. + This will try to get the Materials Project structure and save it, if it does not already exist. """ - structure_path = f'static/assets/data/structures/{structure}.json' - - try: - with open(structure_path, 'r') as f: + structure_path = f"static/assets/data/structures/{structure}.json" + + try: + with open(structure_path, "r") as f: mp_structure = pymatgen.core.Structure.from_dict(json.load(f)) except: with MPRester(MP_API_KEY) as mpr: mp_structure = mpr.get_structure_by_material_id(structure) - with open(structure_path, 'w') as f: + with open(structure_path, "w") as f: json.dump(mp_structure.as_dict(), f) return mp_structure + def create_structure_figure(structure="mp-81", zone_axis=[1, 1, 1]): fig = Figure(dpi=200) axis = fig.add_subplot(1, 1, 1) @@ -41,12 +43,16 @@ def create_structure_figure(structure="mp-81", zone_axis=[1, 1, 1]): crystal = py4DSTEM.process.diffraction.Crystal.from_pymatgen_structure( structure=structure, ) - fig, ax = crystal.plot_structure(returnfig=True, zone_axis_lattice=zone_axis, perspective_axes=True) + fig, ax = crystal.plot_structure( + returnfig=True, zone_axis_lattice=zone_axis, perspective_axes=True + ) ax.set_position([0, 0, 1, 1]) return fig -def create_dp_figure(structure="mp-81", zone_axis=[1, 1, 1], accelerating_voltage=200e3): +def create_dp_figure( + structure="mp-81", zone_axis=[1, 1, 1], accelerating_voltage=200e3 +): fig = Figure(dpi=100) ax = fig.add_subplot(1, 1, 1) structure = get_mp_structure(structure) diff --git a/templates/dp_sim.html b/templates/dp_sim.html index 16f1031..dc0c543 100644 --- a/templates/dp_sim.html +++ b/templates/dp_sim.html @@ -21,8 +21,8 @@ - + @@ -48,7 +48,11 @@ alt="Crystal Structure" /> -{% else %} {% endif %} +{% else %} + +

{{message}}

+ +{% endif %}

See the