te')); return $arr; } /* 遍历用户所有主题 * @param $uid 用户ID * @param int $page 页数 * @param int $pagesize 每页记录条数 * @param bool $desc 排序方式 TRUE降序 FALSE升序 * @param string $key 返回的数组用那一列的值作为 key * @param array $col 查询哪些列 */ function thread_tid_find_by_uid($uid, $page = 1, $pagesize = 1000, $desc = TRUE, $key = 'tid', $col = array()) { if (empty($uid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('uid' => $uid), array('tid' => $orderby), $page, $pagesize, $key, $col); return $arr; } // 遍历栏目下tid 支持数组 $fid = array(1,2,3) function thread_tid_find_by_fid($fid, $page = 1, $pagesize = 1000, $desc = TRUE) { if (empty($fid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('fid' => $fid), array('tid' => $orderby), $page, $pagesize, 'tid', array('tid', 'verify_date')); return $arr; } function thread_tid_delete($tid) { if (empty($tid)) return FALSE; $r = thread_tid__delete(array('tid' => $tid)); return $r; } function thread_tid_count() { $n = thread_tid__count(); return $n; } // 统计用户主题数 大数量下严谨使用非主键统计 function thread_uid_count($uid) { $n = thread_tid__count(array('uid' => $uid)); return $n; } // 统计栏目主题数 大数量下严谨使用非主键统计 function thread_fid_count($fid) { $n = thread_tid__count(array('fid' => $fid)); return $n; } ?>Python script to run Jupyter Notebook in loop and save results to HTML - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Python script to run Jupyter Notebook in loop and save results to HTML - Stack Overflow

programmeradmin3浏览0评论

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
发布评论

评论列表(0)

  1. 暂无评论