hemantn commited on
Commit
05c92c8
·
1 Parent(s): c88d38f

Fix viewer PDB file generation and loading

Browse files
Files changed (2) hide show
  1. js/script.js +4 -2
  2. python/app.py +21 -1
js/script.js CHANGED
@@ -1344,8 +1344,10 @@ echo "Analysis completed! Results saved in analysis/ directory"
1344
  throw new Error(data.error || 'Failed to load viewer PDB');
1345
  }
1346
 
1347
- // Open the dedicated viewer page (bypasses CSP issues)
1348
- window.open('/viewer/viewer_protein_with_ligand.pdb', '_blank');
 
 
1349
 
1350
  } catch (error) {
1351
  console.error('Error previewing solvated protein:', error);
 
1344
  throw new Error(data.error || 'Failed to load viewer PDB');
1345
  }
1346
 
1347
+ // Wait a moment for the file to be written, then open the viewer
1348
+ setTimeout(() => {
1349
+ window.open('/viewer/viewer_protein_with_ligand.pdb', '_blank');
1350
+ }, 1000);
1351
 
1352
  } catch (error) {
1353
  console.error('Error previewing solvated protein:', error);
python/app.py CHANGED
@@ -750,6 +750,19 @@ cmd.quit()
750
  @app.route('/viewer/<filename>')
751
  def viewer(filename):
752
  """Serve NGL viewer page"""
 
 
 
 
 
 
 
 
 
 
 
 
 
753
  return f"""
754
  <!DOCTYPE html>
755
  <html>
@@ -783,6 +796,12 @@ def viewer(filename):
783
 
784
  async function initViewer() {{
785
  try {{
 
 
 
 
 
 
786
  stage = new NGL.Stage("viewport", {{ backgroundColor: "white" }});
787
 
788
  const component = await stage.loadFile("/output/{filename}");
@@ -814,7 +833,8 @@ def viewer(filename):
814
  console.error('Error loading structure:', error);
815
  document.getElementById('viewport').innerHTML =
816
  '<div style="padding: 50px; text-align: center; color: #dc3545;">' +
817
- '<h3>Error loading structure</h3><p>' + error.message + '</p></div>';
 
818
  }}
819
  }}
820
 
 
750
  @app.route('/viewer/<filename>')
751
  def viewer(filename):
752
  """Serve NGL viewer page"""
753
+ # Check if the file exists, if not, try to generate it
754
+ file_path = OUTPUT_DIR / filename
755
+ if not file_path.exists():
756
+ # Try to generate the viewer PDB if it's the specific file we need
757
+ if filename == 'viewer_protein_with_ligand.pdb':
758
+ try:
759
+ # Call the get_viewer_pdb function to generate the file
760
+ result = get_viewer_pdb()
761
+ if result[1] == 200: # Success
762
+ pass # File should now exist
763
+ except:
764
+ pass # Continue anyway
765
+
766
  return f"""
767
  <!DOCTYPE html>
768
  <html>
 
796
 
797
  async function initViewer() {{
798
  try {{
799
+ // Check if file exists first
800
+ const response = await fetch("/output/{filename}");
801
+ if (!response.ok) {{
802
+ throw new Error(`File not found: ${{response.status}} ${{response.statusText}}`);
803
+ }}
804
+
805
  stage = new NGL.Stage("viewport", {{ backgroundColor: "white" }});
806
 
807
  const component = await stage.loadFile("/output/{filename}");
 
833
  console.error('Error loading structure:', error);
834
  document.getElementById('viewport').innerHTML =
835
  '<div style="padding: 50px; text-align: center; color: #dc3545;">' +
836
+ '<h3>Error loading structure</h3><p>' + error.message + '</p>' +
837
+ '<p>Make sure the file exists in the output directory.</p></div>';
838
  }}
839
  }}
840