function analysis_workspace
Flask route handler that renders an analysis workspace interface for a given session, displaying session summary and generated files in a VS Code-like environment.
/tf/active/vicechatdev/full_smartstat/app.py
1640 - 1659
simple
Purpose
This function serves as a web endpoint to display an interactive analysis workspace for a specific analysis session. It retrieves session data including generated files, validates the session exists, and renders a workspace template. If the session is not found or an error occurs, it redirects to the index page with an appropriate error message.
Source Code
def analysis_workspace(session_id):
"""Analysis workspace interface (like VS Code)"""
try:
summary = analysis_service.get_session_summary(session_id)
if not summary['success']:
flash(f"Session not found: {session_id}", 'error')
return redirect(url_for('index'))
# Include generated_files in the session data for the template
summary['generated_files'] = summary.get('generated_files', [])
return render_template('workspace.html',
session_data=summary,
session_id=session_id)
except Exception as e:
logger.error(f"Error loading workspace: {str(e)}")
flash(f"Error loading workspace: {str(e)}", 'error')
return redirect(url_for('index'))
Parameters
| Name | Type | Default | Kind |
|---|---|---|---|
session_id |
- | - | positional_or_keyword |
Parameter Details
session_id: String identifier for the analysis session. Used to retrieve session summary and associated data from the analysis service. Must correspond to an existing session in the system.
Return Value
Returns a Flask Response object. On success, renders 'workspace.html' template with session_data (including generated_files list) and session_id. On failure (session not found or exception), redirects to the index page using url_for('index') and displays a flash message with error details.
Dependencies
flaskloggingpathlib
Required Imports
from flask import render_template
from flask import redirect
from flask import url_for
from flask import flash
import logging
Usage Example
# In Flask application context
# Assuming app, analysis_service, and logger are configured
from flask import Flask, render_template, redirect, url_for, flash
import logging
app = Flask(__name__)
app.secret_key = 'your-secret-key'
logger = logging.getLogger(__name__)
# analysis_service should be initialized
# from services import StatisticalAnalysisService
# analysis_service = StatisticalAnalysisService()
@app.route('/workspace/<session_id>')
def analysis_workspace(session_id):
try:
summary = analysis_service.get_session_summary(session_id)
if not summary['success']:
flash(f"Session not found: {session_id}", 'error')
return redirect(url_for('index'))
summary['generated_files'] = summary.get('generated_files', [])
return render_template('workspace.html',
session_data=summary,
session_id=session_id)
except Exception as e:
logger.error(f"Error loading workspace: {str(e)}")
flash(f"Error loading workspace: {str(e)}", 'error')
return redirect(url_for('index'))
# Access via: http://localhost:5000/workspace/abc123-session-id
Best Practices
- Always validate session_id exists before rendering workspace to prevent displaying invalid data
- Use try-except blocks to catch and handle errors gracefully with user-friendly messages
- Flash messages should be displayed in the template to inform users of errors
- Ensure analysis_service.get_session_summary returns a dictionary with 'success' key for validation
- The 'generated_files' key should be safely accessed with .get() to avoid KeyError
- Logger should be configured at module level for consistent error tracking
- Redirect to index page on errors to provide a safe fallback for users
- Template 'workspace.html' must be prepared to handle session_data and session_id variables
Tags
Similar Components
AI-powered semantic similarity - components with related functionality:
-
function workspace 78.0% similar
-
function smartstat_workspace 74.9% similar
-
function document_workspace 74.9% similar
-
function view_session 74.5% similar
-
function index 70.4% similar