feat: 更通用的 带进度条的 python通信方式
This commit is contained in:
@@ -1,22 +1,16 @@
|
||||
use serde::{Deserialize, Serialize};
|
||||
use crate::python_executor::execute_python_command;
|
||||
use tauri::{AppHandle, Emitter};
|
||||
use crate::python_executor::{execute_python_command, execute_python_with_events};
|
||||
use tauri::AppHandle;
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
pub struct BatchImportRequest {
|
||||
pub source_folder: String,
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize, Clone)]
|
||||
pub struct ImportProgress {
|
||||
pub step: String,
|
||||
pub progress: f64,
|
||||
pub message: String,
|
||||
pub current_template: Option<String>,
|
||||
pub total_templates: Option<i32>,
|
||||
pub processed_templates: Option<i32>,
|
||||
pub timestamp: f64,
|
||||
}
|
||||
// Re-export PythonProgress as ImportProgress for backward compatibility
|
||||
// Note: This is used by frontend TypeScript definitions
|
||||
#[allow(unused_imports)]
|
||||
pub use crate::python_executor::PythonProgress as ImportProgress;
|
||||
|
||||
#[derive(Debug, Serialize)]
|
||||
pub struct TemplateInfo {
|
||||
@@ -65,29 +59,8 @@ pub async fn batch_import_templates_with_progress(
|
||||
request.source_folder,
|
||||
];
|
||||
|
||||
// Create a progress callback that emits events to the frontend
|
||||
let app_clone = app.clone();
|
||||
let progress_callback = move |progress: ImportProgress| {
|
||||
let _ = app_clone.emit("template-import-progress", &progress);
|
||||
};
|
||||
|
||||
// Execute with progress monitoring
|
||||
execute_python_command_with_progress(app, &args, None, progress_callback).await
|
||||
}
|
||||
|
||||
async fn execute_python_command_with_progress<F>(
|
||||
app: AppHandle,
|
||||
args: &[String],
|
||||
config: Option<crate::python_executor::PythonExecutorConfig>,
|
||||
_progress_callback: F,
|
||||
) -> Result<String, String>
|
||||
where
|
||||
F: Fn(ImportProgress) + Send + 'static,
|
||||
{
|
||||
// For now, we'll use the existing execute_python_command
|
||||
// and parse progress from JSON-RPC notifications
|
||||
// TODO: Enhance python_executor to support progress callbacks
|
||||
execute_python_command(app, args, config).await
|
||||
// Execute with progress monitoring via Tauri events
|
||||
execute_python_with_events(app, &args, None, "template-import-progress").await
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
@@ -129,3 +102,28 @@ pub async fn delete_template(app: tauri::AppHandle, template_id: String) -> Resu
|
||||
|
||||
execute_python_command(app, &args, None).await
|
||||
}
|
||||
|
||||
// Example: AI Video generation with progress
|
||||
#[allow(dead_code)]
|
||||
#[tauri::command]
|
||||
pub async fn generate_video_with_progress(
|
||||
app: AppHandle,
|
||||
image_path: String,
|
||||
prompt: String,
|
||||
output_path: String,
|
||||
) -> Result<String, String> {
|
||||
let args = vec![
|
||||
"-m".to_string(),
|
||||
"python_core.ai_video.video_generator".to_string(),
|
||||
"--image_path".to_string(),
|
||||
image_path,
|
||||
"--prompt".to_string(),
|
||||
prompt,
|
||||
"--output_path".to_string(),
|
||||
output_path,
|
||||
];
|
||||
|
||||
// Execute with progress monitoring via Tauri events
|
||||
// Frontend can listen to "video-generation-progress" event
|
||||
execute_python_with_events(app, &args, None, "video-generation-progress").await
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user