alidenewade commited on
Commit
fdc9ad2
·
verified ·
1 Parent(s): f1e0ac0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -17
app.py CHANGED
@@ -318,12 +318,21 @@ def visualize_molecule_2d_3d(smiles: str, name: str):
318
  drawer.drawOptions().backgroundColour = (0.11, 0.11, 0.11) # Dark background
319
  drawer.drawOptions().symbolColour = (1, 1, 1) # White symbols
320
  drawer.drawOptions().defaultColour = (1, 1, 1) # White default color
 
 
 
 
 
 
321
 
322
  drawer.DrawMolecule(mol)
323
  drawer.FinishDrawing()
324
  svg_2d = drawer.GetDrawingText().replace('svg:', '')
325
 
326
- # Comprehensive SVG color fixes for dark background
 
 
 
327
  svg_2d = svg_2d.replace('stroke="black"', 'stroke="white"')
328
  svg_2d = svg_2d.replace('fill="black"', 'fill="white"')
329
  svg_2d = svg_2d.replace('stroke="#000000"', 'stroke="#FFFFFF"')
@@ -336,30 +345,34 @@ def visualize_molecule_2d_3d(smiles: str, name: str):
336
  svg_2d = svg_2d.replace('fill:#000000', 'fill:#FFFFFF')
337
  svg_2d = svg_2d.replace('stroke:#000', 'stroke:#FFF')
338
  svg_2d = svg_2d.replace('fill:#000', 'fill:#FFF')
339
-
340
- # Replace any remaining RGB black values
341
  svg_2d = svg_2d.replace('stroke="rgb(0,0,0)"', 'stroke="rgb(255,255,255)"')
342
  svg_2d = svg_2d.replace('fill="rgb(0,0,0)"', 'fill="rgb(255,255,255)"')
343
  svg_2d = svg_2d.replace('stroke:rgb(0,0,0)', 'stroke:rgb(255,255,255)')
344
  svg_2d = svg_2d.replace('fill:rgb(0,0,0)', 'fill:rgb(255,255,255)')
345
-
346
- # Fix text/font colors for atom labels and stereochemistry labels
347
- svg_2d = svg_2d.replace('<text', '<text fill="white"')
348
- svg_2d = svg_2d.replace('fill="white" fill="white"', 'fill="white"') # Remove duplicates
349
  svg_2d = svg_2d.replace('color="black"', 'color="white"')
350
  svg_2d = svg_2d.replace('color:#000000', 'color:#FFFFFF')
351
  svg_2d = svg_2d.replace('color:#000', 'color:#FFF')
352
 
353
- # Additional fixes for stereochemistry labels (R) and (S) that might be rendered differently
354
- # These labels are often in separate text elements
355
- import re
356
- # Find all text elements and ensure they have white fill
357
- svg_2d = re.sub(r'<text([^>]*?)(?:fill="[^"]*")?([^>]*?)>', r'<text\1 fill="white"\2>', svg_2d)
358
- # Fix any remaining black text in style attributes
359
- svg_2d = re.sub(r'style="([^"]*?)fill:\s*black([^"]*?)"', r'style="\1fill:white\2"', svg_2d)
360
- svg_2d = re.sub(r'style="([^"]*?)fill:\s*#000000([^"]*?)"', r'style="\1fill:#FFFFFF\2"', svg_2d)
361
- svg_2d = re.sub(r'style="([^"]*?)fill:\s*#000([^"]*?)"', r'style="\1fill:#FFF\2"', svg_2d)
362
- svg_2d = re.sub(r'style="([^"]*?)fill:\s*rgb\(0,0,0\)([^"]*?)"', r'style="\1fill:rgb(255,255,255)\2"', svg_2d)
 
 
 
 
 
 
 
 
 
 
363
 
364
  # Embed the SVG within a div with a dark background for consistency
365
  svg_2d = f'<div style="background-color: #1C1C1C; padding: 10px; border-radius: 5px;">{svg_2d}</div>'
 
318
  drawer.drawOptions().backgroundColour = (0.11, 0.11, 0.11) # Dark background
319
  drawer.drawOptions().symbolColour = (1, 1, 1) # White symbols
320
  drawer.drawOptions().defaultColour = (1, 1, 1) # White default color
321
+
322
+ # Try to set annotation color (this might help with (R)/(S) labels)
323
+ try:
324
+ drawer.drawOptions().annotationColour = (1, 1, 1) # White annotations
325
+ except:
326
+ pass
327
 
328
  drawer.DrawMolecule(mol)
329
  drawer.FinishDrawing()
330
  svg_2d = drawer.GetDrawingText().replace('svg:', '')
331
 
332
+ # More aggressive SVG text color fixes - target all possible black text variations
333
+ import re
334
+
335
+ # First, comprehensive string replacements
336
  svg_2d = svg_2d.replace('stroke="black"', 'stroke="white"')
337
  svg_2d = svg_2d.replace('fill="black"', 'fill="white"')
338
  svg_2d = svg_2d.replace('stroke="#000000"', 'stroke="#FFFFFF"')
 
345
  svg_2d = svg_2d.replace('fill:#000000', 'fill:#FFFFFF')
346
  svg_2d = svg_2d.replace('stroke:#000', 'stroke:#FFF')
347
  svg_2d = svg_2d.replace('fill:#000', 'fill:#FFF')
 
 
348
  svg_2d = svg_2d.replace('stroke="rgb(0,0,0)"', 'stroke="rgb(255,255,255)"')
349
  svg_2d = svg_2d.replace('fill="rgb(0,0,0)"', 'fill="rgb(255,255,255)"')
350
  svg_2d = svg_2d.replace('stroke:rgb(0,0,0)', 'stroke:rgb(255,255,255)')
351
  svg_2d = svg_2d.replace('fill:rgb(0,0,0)', 'fill:rgb(255,255,255)')
 
 
 
 
352
  svg_2d = svg_2d.replace('color="black"', 'color="white"')
353
  svg_2d = svg_2d.replace('color:#000000', 'color:#FFFFFF')
354
  svg_2d = svg_2d.replace('color:#000', 'color:#FFF')
355
 
356
+ # Aggressive regex-based fixes for all text elements
357
+ # Remove any existing fill attributes from text elements and add white fill
358
+ svg_2d = re.sub(r'<text([^>]*?)\s+fill="[^"]*"([^>]*?)>', r'<text\1\2 fill="white">', svg_2d)
359
+ svg_2d = re.sub(r'<text([^>]*?)(?<!fill="white")>', r'<text\1 fill="white">', svg_2d)
360
+
361
+ # Fix style attributes in text elements
362
+ svg_2d = re.sub(r'<text([^>]*?)style="([^"]*?)fill:\s*(?:black|#000000|#000|rgb\(0,0,0\))([^"]*?)"([^>]*?)>',
363
+ r'<text\1style="\2fill:white\3"\4>', svg_2d)
364
+
365
+ # If text elements don't have any fill specified, ensure they get white
366
+ svg_2d = re.sub(r'<text(?![^>]*fill=)([^>]*?)>', r'<text fill="white"\1>', svg_2d)
367
+
368
+ # Clean up any duplicate fill attributes
369
+ svg_2d = re.sub(r'fill="white"\s+fill="white"', 'fill="white"', svg_2d)
370
+
371
+ # Final catch-all: replace any remaining black in the entire SVG
372
+ svg_2d = re.sub(r'\bblack\b', 'white', svg_2d)
373
+ svg_2d = re.sub(r'#000000', '#FFFFFF', svg_2d)
374
+ svg_2d = re.sub(r'#000\b', '#FFF', svg_2d)
375
+ svg_2d = re.sub(r'rgb\(0,\s*0,\s*0\)', 'rgb(255,255,255)', svg_2d)
376
 
377
  # Embed the SVG within a div with a dark background for consistency
378
  svg_2d = f'<div style="background-color: #1C1C1C; padding: 10px; border-radius: 5px;">{svg_2d}</div>'