Spaces:
Sleeping
Sleeping
Error fixed
Browse files- Dockerfile +3 -3
- app/__pycache__/__init__.cpython-313.pyc +0 -0
- app/__pycache__/database.cpython-313.pyc +0 -0
- app/__pycache__/main.cpython-313.pyc +0 -0
- app/__pycache__/schemas.cpython-313.pyc +0 -0
- app/main.py +37 -4
- app/static/index.html +4 -2
- app/static/script.js +6 -0
Dockerfile
CHANGED
@@ -10,7 +10,7 @@ COPY ./requirements.txt /app/requirements.txt
|
|
10 |
# Install dependencies from requirements.txt only
|
11 |
RUN pip install --no-cache-dir --upgrade -r requirements.txt
|
12 |
|
13 |
-
# Copy the entire project
|
14 |
COPY . /app
|
15 |
|
16 |
# Create user, directory, and log file with proper ownership in one step
|
@@ -25,5 +25,5 @@ ENV HOME=/home/user \
|
|
25 |
# Expose port
|
26 |
EXPOSE 7860
|
27 |
|
28 |
-
# Command to run the app with debug logging
|
29 |
-
CMD ["sh", "-c", "uvicorn app.main:app --host 0.0.0.0 --port 7860 --log-level debug --log-config log_config.json"]
|
|
|
10 |
# Install dependencies from requirements.txt only
|
11 |
RUN pip install --no-cache-dir --upgrade -r requirements.txt
|
12 |
|
13 |
+
# Copy the entire project including schemas and questions
|
14 |
COPY . /app
|
15 |
|
16 |
# Create user, directory, and log file with proper ownership in one step
|
|
|
25 |
# Expose port
|
26 |
EXPOSE 7860
|
27 |
|
28 |
+
# Command to run the app with debug logging
|
29 |
+
CMD ["sh", "-c", "uvicorn app.main:app --host 0.0.0.0 --port 7860 --log-level debug --log-config log_config.json --access-log"]
|
app/__pycache__/__init__.cpython-313.pyc
CHANGED
Binary files a/app/__pycache__/__init__.cpython-313.pyc and b/app/__pycache__/__init__.cpython-313.pyc differ
|
|
app/__pycache__/database.cpython-313.pyc
CHANGED
Binary files a/app/__pycache__/database.cpython-313.pyc and b/app/__pycache__/database.cpython-313.pyc differ
|
|
app/__pycache__/main.cpython-313.pyc
CHANGED
Binary files a/app/__pycache__/main.cpython-313.pyc and b/app/__pycache__/main.cpython-313.pyc differ
|
|
app/__pycache__/schemas.cpython-313.pyc
CHANGED
Binary files a/app/__pycache__/schemas.cpython-313.pyc and b/app/__pycache__/schemas.cpython-313.pyc differ
|
|
app/main.py
CHANGED
@@ -2,6 +2,7 @@ from fastapi import FastAPI, HTTPException, Header, Request
|
|
2 |
from fastapi.responses import JSONResponse, HTMLResponse
|
3 |
from fastapi.staticfiles import StaticFiles
|
4 |
from pydantic import BaseModel
|
|
|
5 |
import sqlite3
|
6 |
import sqlparse
|
7 |
import os
|
@@ -104,20 +105,52 @@ async def create_session():
|
|
104 |
sessions[session_id] = {"conn": create_session_db(), "domain": None}
|
105 |
return {"session_id": session_id}
|
106 |
|
|
|
107 |
@app.get("/api/databases")
|
108 |
async def get_databases():
|
109 |
questions_dir = os.path.join(BASE_DIR, "questions")
|
110 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
111 |
|
112 |
@app.post("/api/load-schema/{domain}")
|
113 |
async def load_schema(domain: str, session_id: str = Header(...)):
|
114 |
-
|
|
|
|
|
|
|
115 |
sessions[session_id] = {"conn": create_session_db(), "domain": domain}
|
116 |
try:
|
117 |
-
|
|
|
|
|
118 |
sessions[session_id]["conn"].commit()
|
|
|
|
|
|
|
|
|
|
|
|
|
119 |
except sqlite3.Error as e:
|
120 |
-
|
|
|
121 |
del sessions[session_id]
|
122 |
raise HTTPException(status_code=500, detail=f"Database error: {str(e)}")
|
123 |
return {"message": f"Database {domain} loaded"}
|
|
|
2 |
from fastapi.responses import JSONResponse, HTMLResponse
|
3 |
from fastapi.staticfiles import StaticFiles
|
4 |
from pydantic import BaseModel
|
5 |
+
from loguru import logger
|
6 |
import sqlite3
|
7 |
import sqlparse
|
8 |
import os
|
|
|
105 |
sessions[session_id] = {"conn": create_session_db(), "domain": None}
|
106 |
return {"session_id": session_id}
|
107 |
|
108 |
+
|
109 |
@app.get("/api/databases")
|
110 |
async def get_databases():
|
111 |
questions_dir = os.path.join(BASE_DIR, "questions")
|
112 |
+
logger.debug(f"Checking databases in directory: {questions_dir}")
|
113 |
+
if not os.path.exists(questions_dir):
|
114 |
+
logger.error(f"Questions directory not found: {questions_dir}")
|
115 |
+
return {"databases": []}
|
116 |
+
databases = [f.replace(".json", "") for f in os.listdir(questions_dir) if f.endswith(".json")]
|
117 |
+
logger.debug(f"Found databases: {databases}")
|
118 |
+
return {"databases": databases}
|
119 |
+
|
120 |
+
from loguru import logger
|
121 |
+
|
122 |
+
@app.get("/api/databases")
|
123 |
+
async def get_databases():
|
124 |
+
questions_dir = os.path.join(BASE_DIR, "questions")
|
125 |
+
logger.debug(f"Checking databases in directory: {questions_dir}")
|
126 |
+
if not os.path.exists(questions_dir):
|
127 |
+
logger.error(f"Questions directory not found: {questions_dir}")
|
128 |
+
return {"databases": []}
|
129 |
+
databases = [f.replace(".json", "") for f in os.listdir(questions_dir) if f.endswith(".json")]
|
130 |
+
logger.debug(f"Found databases: {databases}")
|
131 |
+
return {"databases": databases}
|
132 |
|
133 |
@app.post("/api/load-schema/{domain}")
|
134 |
async def load_schema(domain: str, session_id: str = Header(...)):
|
135 |
+
logger.debug(f"Loading schema for domain: {domain}, session_id: {session_id}")
|
136 |
+
if session_id not in sessions:
|
137 |
+
logger.error(f"Invalid session: {session_id}")
|
138 |
+
raise HTTPException(status_code=401, detail="Invalid session")
|
139 |
sessions[session_id] = {"conn": create_session_db(), "domain": domain}
|
140 |
try:
|
141 |
+
schema_sql = load_schema_sql(domain)
|
142 |
+
logger.debug(f"Schema SQL loaded for {domain}")
|
143 |
+
sessions[session_id]["conn"].executescript(schema_sql)
|
144 |
sessions[session_id]["conn"].commit()
|
145 |
+
logger.info(f"Schema loaded successfully for {domain}")
|
146 |
+
except FileNotFoundError as e:
|
147 |
+
logger.error(f"Schema file not found: {str(e)}")
|
148 |
+
close_session_db(sessions[session_id]["conn"])
|
149 |
+
del sessions[session_id]
|
150 |
+
raise HTTPException(status_code=500, detail=str(e))
|
151 |
except sqlite3.Error as e:
|
152 |
+
logger.error(f"Database error: {str(e)}")
|
153 |
+
close_session_db(sessions[session_id]["conn"])
|
154 |
del sessions[session_id]
|
155 |
raise HTTPException(status_code=500, detail=f"Database error: {str(e)}")
|
156 |
return {"message": f"Database {domain} loaded"}
|
app/static/index.html
CHANGED
@@ -25,7 +25,9 @@
|
|
25 |
<h1 class="text-3xl font-bold mb-6 text-center text-blue-600">
|
26 |
SQL Practice Platform
|
27 |
</h1>
|
28 |
-
<div
|
|
|
|
|
29 |
<div class="bg-white p-4 rounded-lg shadow-md">
|
30 |
<h2 class="text-xl font-semibold mb-4 text-green-600">Database</h2>
|
31 |
<select
|
@@ -60,7 +62,7 @@
|
|
60 |
</div>
|
61 |
<div class="bg-white p-4 rounded-lg shadow-md flex flex-col">
|
62 |
<h2 class="text-xl font-semibold mb-4 text-purple-600">SQL Editor</h2>
|
63 |
-
<div id="sqlEditor" class="w-full h-
|
64 |
<div class="flex space-x-4 mb-4">
|
65 |
<button
|
66 |
id="runQueryBtn"
|
|
|
25 |
<h1 class="text-3xl font-bold mb-6 text-center text-blue-600">
|
26 |
SQL Practice Platform
|
27 |
</h1>
|
28 |
+
<div
|
29 |
+
class="grid grid-cols-1 md:grid-cols-[30%,40%,30%] gap-4 h-[calc(100vh-100px)]"
|
30 |
+
>
|
31 |
<div class="bg-white p-4 rounded-lg shadow-md">
|
32 |
<h2 class="text-xl font-semibold mb-4 text-green-600">Database</h2>
|
33 |
<select
|
|
|
62 |
</div>
|
63 |
<div class="bg-white p-4 rounded-lg shadow-md flex flex-col">
|
64 |
<h2 class="text-xl font-semibold mb-4 text-purple-600">SQL Editor</h2>
|
65 |
+
<div id="sqlEditor" class="w-full h-72 border rounded-md mb-4"></div>
|
66 |
<div class="flex space-x-4 mb-4">
|
67 |
<button
|
68 |
id="runQueryBtn"
|
app/static/script.js
CHANGED
@@ -31,12 +31,18 @@ async function init() {
|
|
31 |
const domains = (await domainResponse.json()).databases;
|
32 |
const domainSelect = document.getElementById("domainSelect");
|
33 |
domainSelect.innerHTML = '<option value="">Select Database</option>';
|
|
|
|
|
|
|
|
|
34 |
domains.forEach((domain) => {
|
35 |
const option = document.createElement("option");
|
36 |
option.value = domain;
|
37 |
option.textContent = domain.charAt(0).toUpperCase() + domain.slice(1);
|
38 |
domainSelect.appendChild(option);
|
39 |
});
|
|
|
|
|
40 |
document
|
41 |
.getElementById("loadSchemaBtn")
|
42 |
.addEventListener("click", loadDomain);
|
|
|
31 |
const domains = (await domainResponse.json()).databases;
|
32 |
const domainSelect = document.getElementById("domainSelect");
|
33 |
domainSelect.innerHTML = '<option value="">Select Database</option>';
|
34 |
+
if (domains.length === 0) {
|
35 |
+
showError("No databases available. Check server configuration.");
|
36 |
+
return;
|
37 |
+
}
|
38 |
domains.forEach((domain) => {
|
39 |
const option = document.createElement("option");
|
40 |
option.value = domain;
|
41 |
option.textContent = domain.charAt(0).toUpperCase() + domain.slice(1);
|
42 |
domainSelect.appendChild(option);
|
43 |
});
|
44 |
+
// Enable the load button after populating domains
|
45 |
+
document.getElementById("loadSchemaBtn").disabled = false;
|
46 |
document
|
47 |
.getElementById("loadSchemaBtn")
|
48 |
.addEventListener("click", loadDomain);
|