Erstellen Sie eine Python -Datei, mit der Jupyter -Notizbuch in Schleife ausgeführt wird, und speichern Sie die ErgebnisPython

Python-Programme
Anonymous
 Erstellen Sie eine Python -Datei, mit der Jupyter -Notizbuch in Schleife ausgeführt wird, und speichern Sie die Ergebnis

Post by Anonymous »

Derzeit habe ich eine Jupyter -Notebook -Datenanalyse, die ich für mehrere Länder betreibe, und das einzige, was ich tun muss, ist die Variable "Land" zu ersetzen, die ich mit dem Land, das ich möchte, die Analyse vorliegt. Nach Abschluss der Analyse muss ich die Jupyter speichern und diesen Code erst dann ausführen: < /p>

Code: Select all

!jupyter nbconvert DataAnalysis.ipynb --no-input --no-prompt --to html
< /code>
Dies würde eine HTML -Datei generieren, die ich umbenennen muss, und dann die Variable "Country" für ein anderes Land erneut ändern, das Skript ausführen (neben der letzten Zeile, die kommentiert wird), speichern Sie das Ergebnis Nachdem Diagramme angezeigt wurden, und die letzte Zeile erneut und so weiter ausführen. Land Es ist) in einem "Berichte" -Firde in Schleife, versuchte dies mit Hilfe von Chatgpt, aber keine Ahnung, was zu tun ist: < /p>
    import os

# List of countries for which reports are needed
countries = ['Czech Queue','Switzerland Queue','Netherlands Queue','Portugal Queue','Peru Queue','London Queue','Sweden Queue','Slovakia Queue','Finland Queue','Denmark Queue','Norway Queue','Spain Queue','France Queue']

notebook_input = "DataAnalysis.ipynb"  # Your original notebook
output_dir = "Reports"
os.makedirs(output_dir, exist_ok=True)

for country in countries:
output_notebook = os.path.join(output_dir, f"{country.replace(' ', '_')}_Report.ipynb")
output_html = os.path.join(output_dir, f"{country.replace(' ', '_')}_Report.html")

print(f"Generating report for {country}...")

# Read the notebook content
with open(notebook_input, "r", encoding="utf-8") as f:
notebook_content = f.read()

# Replace any existing "country =" assignment to the correct country
notebook_content = notebook_content.replace('country = ', f'country = "{country}" #')

# Write the updated notebook
with open(output_notebook, "w", encoding="utf-8") as f:
f.write(notebook_content)

# Execute the notebook in place
os.system(f"jupyter nbconvert --execute --inplace {output_notebook}")

# Convert executed notebook to HTML
os.system(f"jupyter nbconvert {output_notebook} --no-input --no-prompt --to html --output {output_html}")

print("All reports generated successfully!")
Eine Idee, wie dies erreichbar sein könnte?

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post