Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
math_tutor_dev
public_math_tutor
Commits
57c8f712
Commit
57c8f712
authored
Jan 26, 2026
by
Kantz
Browse files
spiechern überarbeitet sodass es nun auch "nicht-mathe" uploads korrekt speichert.
parent
925d5684
Changes
1
Show whitespace changes
Inline
Side-by-side
math-tutor/backend/app/api/canvas.py
View file @
57c8f712
from
__future__
import
annotations
from
__future__
import
annotations
import
base64
import
base64
import
os
import
time
import
time
from
pathlib
import
Path
from
pathlib
import
Path
from
typing
import
Optional
from
typing
import
Optional
import
logging
from
app
import
config
from
app
import
config
from
fastapi
import
APIRouter
,
HTTPException
from
fastapi
import
APIRouter
,
HTTPException
from
pydantic
import
BaseModel
,
Field
from
pydantic
import
BaseModel
,
Field
...
@@ -14,7 +13,6 @@ from mpxpy.mathpix_client import MathpixClient
...
@@ -14,7 +13,6 @@ from mpxpy.mathpix_client import MathpixClient
router
=
APIRouter
()
router
=
APIRouter
()
logger
=
logging
.
getLogger
(
__name__
)
settings
=
config
.
get_mathpix_settings
()
settings
=
config
.
get_mathpix_settings
()
...
@@ -57,22 +55,28 @@ def save_canvas(request: CanvasSaveRequest) -> CanvasSaveResponse:
...
@@ -57,22 +55,28 @@ def save_canvas(request: CanvasSaveRequest) -> CanvasSaveResponse:
if
not
safe_hint
:
if
not
safe_hint
:
safe_hint
=
"drawing"
safe_hint
=
"drawing"
filename
=
f
"
{
safe_hint
}
-
{
timestamp
}
.png"
base_name
=
f
"
{
safe_hint
}
-
{
timestamp
}
"
file_path
=
drawings_dir
/
filename
img_path
=
drawings_dir
/
f
"
{
base_name
}
.png"
txt_path
=
drawings_dir
/
f
"
{
base_name
}
.txt"
with
file
_path
.
open
(
"wb"
)
as
handle
:
with
img
_path
.
open
(
"wb"
)
as
handle
:
handle
.
write
(
raw
)
handle
.
write
(
raw
)
try
:
try
:
image
=
mathpix_client
.
image_new
(
file_path
=
str
(
file
_path
),
include_line_data
=
True
)
image
=
mathpix_client
.
image_new
(
file_path
=
str
(
img
_path
),
include_line_data
=
True
)
lines
=
image
.
lines_json
()
lines
=
image
.
lines_json
()
logger
.
info
(
"Mathpix lines: %s"
,
lines
)
line_type
=
lines
[
0
][
"type"
]
if
lines
else
None
line_type
=
lines
[
0
][
"type"
]
if
lines
else
None
if
line_type
!=
"math"
:
if
line_type
!=
"math"
:
nf_img_path
=
img_path
.
with_name
(
f
"nf_
{
img_path
.
name
}
"
)
nf_txt_path
=
txt_path
.
with_name
(
f
"nf_
{
txt_path
.
name
}
"
)
os
.
rename
(
img_path
,
nf_img_path
)
nf_txt_path
.
write_text
(
lines
[
0
][
"text"
]
)
return
CanvasSaveResponse
(
return
CanvasSaveResponse
(
status
=
"error"
,
status
=
"error"
,
latex
=
f
"Please provide a math-formula, you provided a
{
line_type
}
."
,
latex
=
f
"Please provide a math-formula, you provided a
{
line_type
}
."
,
saved_as
=
str
(
file
_path
),
saved_as
=
str
(
nf_img
_path
),
)
)
mmd
=
image
.
mmd
()
mmd
=
image
.
mmd
()
conversion
=
mathpix_client
.
conversion_new
(
conversion
=
mathpix_client
.
conversion_new
(
...
@@ -84,8 +88,10 @@ def save_canvas(request: CanvasSaveRequest) -> CanvasSaveResponse:
...
@@ -84,8 +88,10 @@ def save_canvas(request: CanvasSaveRequest) -> CanvasSaveResponse:
except
Exception
as
exc
:
except
Exception
as
exc
:
raise
HTTPException
(
status_code
=
500
,
detail
=
"mathpix failed"
)
from
exc
raise
HTTPException
(
status_code
=
500
,
detail
=
"mathpix failed"
)
from
exc
txt_path
.
write_text
(
latex
,
encoding
=
"utf-8"
)
return
CanvasSaveResponse
(
return
CanvasSaveResponse
(
status
=
"ok"
,
status
=
"ok"
,
latex
=
latex
,
latex
=
latex
,
saved_as
=
str
(
file
_path
),
saved_as
=
str
(
img
_path
),
)
)
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment