170 lines
4.9 KiB
Python
170 lines
4.9 KiB
Python
#!/usr/bin/env python3
|
||
"""
|
||
STT Jupyter Notebook使用示例
|
||
|
||
这个脚本展示了如何在Jupyter notebook中使用STT的Python绑定,
|
||
包括进度条适配。
|
||
"""
|
||
|
||
import pystt as stt
|
||
import sys
|
||
import os
|
||
|
||
def basic_jupyter_example():
|
||
"""Jupyter notebook基本使用示例"""
|
||
print("=== Jupyter Notebook基本示例 ===")
|
||
|
||
# 创建生成器
|
||
generator = stt.SttGenerator()
|
||
|
||
# 设置参数
|
||
generator.set_tree_depth(3, 6)
|
||
generator.set_reference_system("WGS84")
|
||
|
||
# 创建简单的进度回调
|
||
def simple_progress(description, percentage):
|
||
print(f"{description}: {percentage:.1f}%")
|
||
|
||
generator.set_progress_callback(simple_progress)
|
||
|
||
# 运行生成
|
||
result = generator.run("jupyter_output.msh")
|
||
|
||
print(f"生成结果: {'成功' if result == 0 else '失败'}")
|
||
print()
|
||
|
||
def tqdm_jupyter_example():
|
||
"""使用tqdm的Jupyter示例"""
|
||
print("=== 使用tqdm的Jupyter示例 ===")
|
||
|
||
if not stt.HAS_TQDM:
|
||
print("tqdm未安装,使用简单进度条")
|
||
progress_cb = stt.create_simple_callback("STT生成")
|
||
else:
|
||
print("使用tqdm进度条")
|
||
progress_cb = stt.create_tqdm_callback("STT生成")
|
||
|
||
generator = stt.SttGenerator()
|
||
generator.set_progress_callback(progress_cb)
|
||
|
||
generator.set_tree_depth(2, 5)
|
||
generator.set_reference_system("Earth")
|
||
|
||
result = generator.run("tqdm_output.msh")
|
||
|
||
print(f"生成结果: {'成功' if result == 0 else '失败'}")
|
||
print()
|
||
|
||
def auto_progress_example():
|
||
"""自动选择最佳进度条"""
|
||
print("=== 自动选择进度条示例 ===")
|
||
|
||
# 自动创建最适合的进度回调
|
||
progress_cb = stt.create_progress_callback("自动进度")
|
||
|
||
generator = stt.SttGenerator()
|
||
generator.set_progress_callback(progress_cb)
|
||
|
||
generator.set_tree_depth(2, 4)
|
||
generator.set_reference_system("Moon")
|
||
|
||
result = generator.run("auto_output.msh")
|
||
|
||
print(f"生成结果: {'成功' if result == 0 else '失败'}")
|
||
print()
|
||
|
||
def advanced_jupyter_example():
|
||
"""高级Jupyter示例"""
|
||
print("=== 高级Jupyter示例 ===")
|
||
|
||
# 创建tqdm进度回调
|
||
if stt.HAS_TQDM:
|
||
progress_cb = stt.TqdmProgressCallback("高级STT生成")
|
||
else:
|
||
progress_cb = stt.SimpleProgressCallback("高级STT生成")
|
||
|
||
generator = stt.SttGenerator()
|
||
generator.set_progress_callback(progress_cb)
|
||
|
||
# 设置自定义参考系统
|
||
generator.set_pole_equator_radius(3396200.0, 3376200.0) # 火星
|
||
|
||
# 设置二十面体方向
|
||
generator.set_icosahedron_orient(0.0, 90.0)
|
||
|
||
# 使用完整参数
|
||
params = {
|
||
"output_msh": "mars_grid.msh",
|
||
"output_vertex": "mars_vertices.txt",
|
||
"output_triangle_center": "mars_centers.txt"
|
||
}
|
||
|
||
result = generator.run_full(params)
|
||
|
||
print(f"火星网格生成: {'成功' if result == 0 else '失败'}")
|
||
print()
|
||
|
||
def notebook_integration_demo():
|
||
"""Notebook集成演示"""
|
||
print("=== Notebook集成演示 ===")
|
||
|
||
# 检查是否在notebook环境中
|
||
try:
|
||
from IPython.display import display, HTML
|
||
in_notebook = True
|
||
except ImportError:
|
||
in_notebook = False
|
||
|
||
if in_notebook:
|
||
print("检测到Jupyter环境,使用增强进度显示")
|
||
display(HTML("<h4>STT生成进度</h4>"))
|
||
|
||
def notebook_progress(description, percentage):
|
||
display(HTML(f"""
|
||
<div style='border: 1px solid #ccc; padding: 10px; margin: 5px;'>
|
||
<strong>{description}</strong>: {percentage:.1f}%
|
||
<div style='background: #f0f0f0; height: 20px; margin-top: 5px;'>
|
||
<div style='background: #4CAF50; height: 100%; width: {percentage}%;'></div>
|
||
</div>
|
||
</div>
|
||
"""))
|
||
|
||
generator = stt.SttGenerator()
|
||
generator.set_progress_callback(notebook_progress)
|
||
else:
|
||
print("普通Python环境,使用标准进度条")
|
||
progress_cb = stt.create_progress_callback("Notebook演示")
|
||
generator = stt.SttGenerator()
|
||
generator.set_progress_callback(progress_cb)
|
||
|
||
generator.set_tree_depth(2, 4)
|
||
generator.set_reference_system("WGS84")
|
||
|
||
result = generator.run("notebook_output.msh")
|
||
|
||
print(f"生成结果: {'成功' if result == 0 else '失败'}")
|
||
print()
|
||
|
||
def main():
|
||
"""主函数"""
|
||
print("STT Jupyter Notebook使用示例")
|
||
print("=" * 40)
|
||
|
||
# 运行各种示例
|
||
try:
|
||
basic_jupyter_example()
|
||
tqdm_jupyter_example()
|
||
auto_progress_example()
|
||
advanced_jupyter_example()
|
||
notebook_integration_demo()
|
||
|
||
print("所有Jupyter示例运行完成!")
|
||
print("检查生成的文件以查看结果。")
|
||
|
||
except Exception as e:
|
||
print(f"运行示例时出错: {e}")
|
||
import traceback
|
||
traceback.print_exc()
|
||
|
||
if __name__ == "__main__":
|
||
main() |