Currently I have a jupyter notebook data analysis that I run for multiple countries and the only thing I need to do is replace the "country" variable on top with the country I desire to run the analysis. After analysis is complete, I have to save the jupyter and only then run this code:
!jupyter nbconvert OneHRHelpDeskTicketCountryReport.ipynb --no-input --no-prompt --to html
This would generate an HTML file I have to rename, then change the "country" variable again for another country, run the script (besides last line that is commented out), save the result after graphs are displayed, and run the last line again and so on.
I want to create a Python file that would execute this Jupyter and save the reports (with any name, just identifying which country it is) in a "Reports" folder in loop, tried this with help of chatgpt but have no idea what to do, it is generating jupyter notebook files .ipynb in the "Report" folder but when I see it the "country = " is not being replaced by anything, it is the same jupyter for all countries. Also not saved and converted to HTML:
import os
import re
# List of countries for which reports are needed
countries = ['HQ Queue', 'Czech Queue', 'Switzerland Queue', 'TMC Queue', 'AMS Queue', 'Netherlands Queue', 'Portugal Queue',
'Peru Queue', 'London Queue', 'Sweden Queue', 'Slovakia Queue', 'Finland Queue', 'Denmark Queue', 'UAE Queue',
'Norway Queue', 'Spain Queue', 'York Queue', 'France Queue']
notebook_input = "OneHRHelpDeskTicketCountryReport.ipynb" # Your original notebook
output_dir = "Reports"
os.makedirs(output_dir, exist_ok=True)
print(f"Saving reports to: {os.path.abspath(output_dir)}")
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"\nGenerating report for {country}...")
# Read the notebook content
with open(notebook_input, "r", encoding="utf-8") as f:
notebook_content = f.read()
# Replace the existing "country =" assignment properly
notebook_content = re.sub(r'country\s*=\s*".*?"', f'country = "{country}"', notebook_content)
# Write the updated notebook
with open(output_notebook, "w", encoding="utf-8") as f:
f.write(notebook_content)
# Execute the notebook
exit_code = os.system(f"jupyter nbconvert --execute --inplace {output_notebook}")
if exit_code != 0:
print(f"Error executing notebook for {country}. Check the notebook manually.")
continue
# Convert executed notebook to HTML
os.system(f"jupyter nbconvert {output_notebook} --no-input --no-prompt --to html --output={output_html}")
print("\nAll reports generated successfully!")
input("\nPress Enter to exit...") # Prevents the command prompt from closing immediately
I need a Python script that does the following:
- Open my Jupyter Notebook file;
- Find the "country =" which should be in the first line and add one of the "countries" in the "countries" list
- Run the Jupyter notebook so graphs and tables are displayed
- Save the new displayed graphs
- Print the page in HTML (before using this line on Jupyter itself, now it is commented out !jupyter nbconvert OneHRHelpDeskTicketCountryReport.ipynb --no-input --no-prompt --to html)
- Do it again for each country on the countries list