Spaces:
Sleeping
Sleeping
| import subprocess | |
| from pathlib import Path | |
| def validate_rmd(path): | |
| rmd_path = Path(path) | |
| if not rmd_path.is_file(): | |
| raise FileNotFoundError(f"File non trovato: {path}") | |
| report_dir = rmd_path.parent | |
| status = 200 | |
| error_message = "" | |
| render_cmd = [ | |
| "Rscript", "--vanilla", "-e", | |
| f"rmarkdown::render(" | |
| f"input = '{rmd_path.as_posix()}', " | |
| f"output_format = c('html_document', 'latex_document'), " | |
| f"output_dir = '{report_dir.as_posix()}')" | |
| ] | |
| try: | |
| result = subprocess.run( | |
| render_cmd, | |
| check=True, | |
| capture_output=True, | |
| text=True, | |
| encoding='utf-8' | |
| ) | |
| base_name = rmd_path.stem | |
| html_file = report_dir / f"{base_name}.html" | |
| tex_file = report_dir / f"{base_name}.tex" | |
| if not (html_file.exists() and tex_file.exists()): | |
| status = 404 | |
| error_message = "File di output non generati correttamente" | |
| except subprocess.CalledProcessError as e: | |
| status = 404 | |
| error_message = f"Errore durante il rendering:\n{e.stderr}" | |
| except Exception as e: | |
| status = 404 | |
| error_message = f"Errore imprevisto: {str(e)}" | |
| status_file = report_dir / str(status) | |
| with open(status_file, 'w', encoding='utf-8') as f: | |
| if status == 404: | |
| f.write(error_message) | |
| return status, error_message if status == 404 else "OK" | |
| def render_rmd_format(path, output_format): | |
| rmd_path = Path(path) | |
| if not rmd_path.is_file(): | |
| raise FileNotFoundError(f"File non trovato: {path}") | |
| report_dir = rmd_path.parent | |
| render_cmd = [ | |
| "Rscript", "--vanilla", "-e", | |
| f"rmarkdown::render(" | |
| f"input = '{rmd_path.as_posix()}', " | |
| f"output_format = '{output_format}', " | |
| f"output_dir = '{report_dir.as_posix()}')" | |
| ] | |
| result = subprocess.run( | |
| render_cmd, | |
| check=True, | |
| capture_output=True, | |
| text=True, | |
| encoding='utf-8' | |
| ) | |
| return result | |