utils.py 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. import streamlit as st
  2. import sqlite3
  3. import pandas as pd
  4. import os
  5. @st.cache_resource
  6. def get_database_connection():
  7. # Assuming the script is run from the dashboard directory,
  8. # evals.db is two levels up from there.
  9. # __file__ is utils.py, its dirname is dashboard.
  10. # os.path.dirname(__file__) -> dashboard/
  11. # os.path.join(..., '..') -> diff-edits/
  12. # os.path.join(..., '..', 'evals.db') -> diff-edits/evals.db
  13. db_path = os.path.join(os.path.dirname(__file__), '..', 'evals.db')
  14. if not os.path.exists(db_path):
  15. st.error(f"Database not found. Expected at: {os.path.abspath(db_path)}")
  16. st.stop()
  17. return sqlite3.connect(db_path, check_same_thread=False)
  18. def guess_language_from_filepath(filepath):
  19. """Guess the language for syntax highlighting from filepath."""
  20. if not filepath or pd.isna(filepath):
  21. return None
  22. extension_map = {
  23. '.py': 'python',
  24. '.js': 'javascript',
  25. '.ts': 'typescript',
  26. '.java': 'java',
  27. '.cs': 'csharp',
  28. '.cpp': 'cpp',
  29. '.c': 'c',
  30. '.html': 'html',
  31. '.css': 'css',
  32. '.json': 'json',
  33. '.sql': 'sql',
  34. '.md': 'markdown',
  35. '.rb': 'ruby',
  36. '.php': 'php',
  37. '.go': 'go',
  38. '.rs': 'rust',
  39. '.swift': 'swift',
  40. '.kt': 'kotlin',
  41. '.sh': 'bash',
  42. '.yaml': 'yaml',
  43. '.yml': 'yaml',
  44. '.xml': 'xml',
  45. }
  46. _, ext = os.path.splitext(str(filepath)) # Ensure filepath is string
  47. return extension_map.get(ext.lower(), None)