Promotion: Exportfunktion für scp-Export in export_figure_local integriert

This commit is contained in:
2025-07-29 19:37:43 +02:00
parent 89887edc1c
commit 6830dd4531
3 changed files with 30 additions and 12 deletions

View File

@ -3,10 +3,18 @@ import os
# Neue Exportfunktion: HTML in /tmp speichern, per SCP übertragen, PNG lokal speichern # Neue Exportfunktion: HTML in /tmp speichern, per SCP übertragen, PNG lokal speichern
def export_figure(fig, name, export_flag_html, export_flag_png): def export_figure(fig, name, export_flag_html, export_flag_png):
from config_korrelation import export_path_png # Lokale Implementierung der Exportlogik inkl. Bib-Quelle im Titel und Dateinamen
safe_filename = slugify(name) from config_korrelation import export_path_png, bib_filename
from slugify import slugify
remote_path = "johajo@sternenflottenakademie.local:/mnt/deep-space-nine/public/plot/promotion/" remote_path = "johajo@sternenflottenakademie.local:/mnt/deep-space-nine/public/plot/promotion/"
# Titel ggf. um Quelle ergänzen
if fig.layout.title and fig.layout.title.text:
if f"| Quelle: {bib_filename.replace('.bib', '')}" not in fig.layout.title.text:
fig.update_layout(title_text=f"{fig.layout.title.text} | Quelle: {bib_filename.replace('.bib', '')}")
safe_filename = slugify(f"{name}_{bib_filename.replace('.bib', '')}")
if export_flag_html: if export_flag_html:
export_path_html = f"/tmp/{safe_filename}.html" export_path_html = f"/tmp/{safe_filename}.html"
fig.write_html(export_path_html, full_html=True, include_plotlyjs="cdn") fig.write_html(export_path_html, full_html=True, include_plotlyjs="cdn")
@ -20,10 +28,10 @@ def export_figure(fig, name, export_flag_html, export_flag_png):
print(e.stderr) print(e.stderr)
if export_flag_png: if export_flag_png:
export_path_png = os.path.join(export_path_png, f"{safe_filename}.png") export_path_png_full = os.path.join(export_path_png, f"{safe_filename}.png")
try: try:
fig.write_image(export_path_png, width=1200, height=800, scale=2) fig.write_image(export_path_png_full, width=1200, height=800, scale=2)
print(f"✅ PNG-Datei lokal gespeichert: '{export_path_png}'") print(f"✅ PNG-Datei lokal gespeichert: '{export_path_png_full}'")
except Exception as e: except Exception as e:
print("❌ Fehler beim PNG-Export:", str(e)) print("❌ Fehler beim PNG-Export:", str(e))
@ -702,11 +710,14 @@ summary_df = pd.DataFrame({
# Plotly-Version für interaktive Darstellung # Plotly-Version für interaktive Darstellung
def plot_average_correlation_plotly(summary_df): def plot_average_correlation_plotly(summary_df):
from config_korrelation import export_path_png
from config_korrelation import bib_filename as global_bib_filename
from slugify import slugify
fig = px.bar( fig = px.bar(
summary_df, summary_df,
x="Korrelationstyp", x="Korrelationstyp",
y="Durchschnittliche Korrelation", y="Durchschnittliche Korrelation",
title=f"Durchschnittliche Korrelationen pro Korrelationstyp (n={len(summary_df)}, Stand: {current_date}) | Quelle: {bib_filename.replace('.bib', '')}", title=f"Durchschnittliche Korrelationen pro Korrelationstyp (n={len(summary_df)}, Stand: {current_date}) | Quelle: {global_bib_filename.replace('.bib', '')}",
labels={"Korrelationstyp": "Korrelationstyp", "Durchschnittliche Korrelation": "Durchschnittliche Korrelation"}, labels={"Korrelationstyp": "Korrelationstyp", "Durchschnittliche Korrelation": "Durchschnittliche Korrelation"},
color="Durchschnittliche Korrelation", color="Durchschnittliche Korrelation",
color_continuous_scale=[ color_continuous_scale=[
@ -718,7 +729,7 @@ def plot_average_correlation_plotly(summary_df):
fig.update_layout( fig.update_layout(
get_standard_layout( get_standard_layout(
title=f"Durchschnittliche Korrelationen pro Korrelationstyp (n={len(summary_df)}, Stand: {current_date}) | Quelle: {bib_filename.replace('.bib', '')}", title=f"Durchschnittliche Korrelationen pro Korrelationstyp (n={len(summary_df)}, Stand: {current_date}) | Quelle: {global_bib_filename.replace('.bib', '')}",
x_title="Korrelationstyp", x_title="Korrelationstyp",
y_title="Durchschnittliche Korrelation" y_title="Durchschnittliche Korrelation"
), ),
@ -740,6 +751,10 @@ def plot_average_correlation_plotly(summary_df):
"summary_plot", "summary_plot",
export_fig_summary_plot export_fig_summary_plot
) )
# PNG-Export ergänzen
png_path = os.path.join(export_path_png, f"{slugify('summary_plot_' + global_bib_filename.replace('.bib', ''))}.png")
fig.write_image(png_path, width=1200, height=800, scale=2)
print(f"✅ PNG-Summary-Datei gespeichert unter: {png_path}")
#============================ #============================
# Aufruf Alle möglichen bivariaten Korrelationen visualisieren # Aufruf Alle möglichen bivariaten Korrelationen visualisieren

View File

@ -66,18 +66,20 @@ from config_netzwerk import export_fig_png
def export_figure_local(fig, name, flag, bib_filename=None): def export_figure_local(fig, name, flag, bib_filename=None):
from config_netzwerk import export_path_html, export_path_png from config_netzwerk import export_path_html, export_path_png
# Einmalige Definition von safe_filename am Anfang der Funktion
safe_filename = prepare_figure_export(fig, name).replace(".html", "")
if flag: if flag:
local_tmp_path = f"/tmp/{slugify(name)}.html" local_tmp_path = f"/tmp/{safe_filename}.html"
fig.write_html(local_tmp_path, full_html=True, include_plotlyjs="cdn") fig.write_html(local_tmp_path, full_html=True, include_plotlyjs="cdn")
try: try:
subprocess.run(["scp", local_tmp_path, f"{export_path_html}/{slugify(name)}.html"], check=True) subprocess.run(["scp", local_tmp_path, f"{export_path_html}/{safe_filename}.html"], check=True)
print(f"✅ HTML-Datei erfolgreich übertragen.") print(f"✅ HTML-Datei erfolgreich übertragen.")
os.remove(local_tmp_path) os.remove(local_tmp_path)
print(f"🗑️ Lokale HTML-Datei '{local_tmp_path}' wurde gelöscht.") print(f"🗑️ Lokale HTML-Datei '{local_tmp_path}' wurde gelöscht.")
except subprocess.CalledProcessError as e: except subprocess.CalledProcessError as e:
print("❌ Fehler bei der Übertragung via SCP:", e) print("❌ Fehler bei der Übertragung via SCP:", e)
if export_fig_png: if export_fig_png:
png_path = os.path.join(export_path_png, f"{slugify(name)}.png") png_path = os.path.join(export_path_png, f"{safe_filename}.png")
try: try:
fig.write_image(png_path, width=1200, height=800, scale=2) fig.write_image(png_path, width=1200, height=800, scale=2)
print(f"✅ PNG-Datei exportiert nach: {png_path}") print(f"✅ PNG-Datei exportiert nach: {png_path}")

View File

@ -152,8 +152,8 @@ fig.update_layout(**layout)
# --- Export-Funktion --- # --- Export-Funktion ---
def export_figure(fig, name, export_flag_html, export_flag_png): def export_figure(fig, name, export_flag_html, export_flag_png):
from config_deskriptive_literaturauswahl import export_path_png, export_path_html from slugify import slugify
safe_name = name.replace(" ", "_").replace("/", "_").lower() safe_name = slugify(name)
html_path = f"/tmp/{safe_name}.html" html_path = f"/tmp/{safe_name}.html"
if export_flag_html: if export_flag_html:
fig.write_html(html_path, include_plotlyjs='cdn', config={"responsive": True}) fig.write_html(html_path, include_plotlyjs='cdn', config={"responsive": True})
@ -175,4 +175,5 @@ def export_figure(fig, name, export_flag_html, export_flag_png):
# --- Export --- # --- Export ---
export_figure(fig, "silhouette_scores_und_fallzahlen", export_fig_silhouette_plot, export_fig_png) export_figure(fig, "silhouette_scores_und_fallzahlen", export_fig_silhouette_plot, export_fig_png)
fig.show() fig.show()