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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
{% extends "base.html" %}

{% block title %}Math Equation to LaTeX | TexLab{% endblock %}

{% block extra_css %}
<style>

  .feature-card {

    display: inline-block;

    width: 100%;

    height: auto !important;

    padding: 20px 25px;

    margin-bottom: 20px;

    background: #fff;

    border-radius: 12px;

    box-shadow: 0 4px 10px rgba(0, 0, 0, 0.05);

  }



  .upload-message {

    display: none;

    background: linear-gradient(135deg, #e3f2fd, #bbdefb);

    /* soft sky-blue gradient */

    color: #0d47a1;

    /* deep blue text */

    font-weight: 600;

    padding: 12px 20px;

    margin: 15px auto;

    text-align: center;

    border-radius: 10px;

    box-shadow: 0 3px 8px rgba(0, 0, 0, 0.08);

    width: fit-content;

    max-width: 80%;

    animation: fadeIn 0.4s ease-in-out;

  }



  /* Smooth fade-in animation */

  @keyframes fadeIn {

    from {

      opacity: 0;

      transform: translateY(-5px);

    }



    to {

      opacity: 1;

      transform: translateY(0);

    }

  }





  .upload-area {

    border: 3px dashed #ddd;

    border-radius: 15px;

    padding: 40px 20px;

    text-align: center;

    transition: all 0.3s ease;

    cursor: pointer;

    background-color: #fafafa;

    margin-bottom: 20px;

  }



  .upload-area:hover {

    border-color: #667eea;

    background-color: #f0f4ff;

  }



  .upload-area.dragover {

    border-color: #667eea;

    background-color: #e0e7ff;

    transform: scale(1.02);

  }



  .upload-icon {

    font-size: 3rem;

    color: #667eea;

    margin-bottom: 15px;

  }



  .upload-text {

    font-size: 1.2rem;

    color: #555;

    margin-bottom: 10px;

  }



  .upload-hint {

    color: #888;

    font-size: 0.9rem;

  }



  .image-preview {

    max-width: 100%;

    max-height: 400px;

    margin: 20px auto;

    display: block;

    border-radius: 10px;

    box-shadow: 0 5px 15px rgba(0, 0, 0, 0.1);

  }



  .latex-output {

    background: #f8f9fa;

    border-radius: 10px;

    padding: 20px;

    border: 1px solid #eee;

    font-family: 'Consolas', 'Courier New', monospace;

    font-size: 1.1rem;

    white-space: pre-wrap;

    word-break: break-all;

    min-height: 100px;

    margin: 20px 0;

  }



  .rendered-output {

    background: white;

    border-radius: 10px;

    padding: 20px;

    border: 1px solid #eee;

    min-height: 100px;

    margin: 20px 0;

    text-align: center;

  }



  .solution-output {

    background: #e8f5e9;

    border-radius: 10px;

    padding: 20px;

    border: 1px solid #c8e6c9;

    margin: 20px 0;

    display: none;

  }



  .solution-title {

    color: #2e7d32;

    margin-bottom: 15px;

    font-weight: 600;

  }



  .solution-content {

    font-family: 'Consolas', 'Courier New', monospace;

    font-size: 1.1rem;

    white-space: pre-wrap;

  }



  .loading {

    display: none;

    text-align: center;

    padding: 20px;

  }



  .spinner {

    border: 4px solid rgba(0, 0, 0, 0.1);

    border-radius: 50%;

    border-top: 4px solid #667eea;

    width: 40px;

    height: 40px;

    animation: spin 1s linear infinite;

    margin: 0 auto 15px;

  }



  @keyframes spin {

    0% {

      transform: rotate(0deg);

    }



    100% {

      transform: rotate(360deg);

    }

  }



  .button-row {

    display: flex;

    gap: 15px;

    justify-content: center;

    margin: 20px 0;

    flex-wrap: wrap;

  }



  @media (max-width: 768px) {

    .button-row {

      flex-direction: column;

      align-items: center;

    }



    .button-row .btn {

      width: 100%;

      max-width: 300px;

    }

  }

</style>
{% endblock %}

{% block content %}
<div class="page-header">
  <h1 class="page-title"><i class="fas fa-square-root-alt me-2"></i>Math Equation to LaTeX</h1>
  <p class="page-subtitle">
    Upload an image of a mathematical equation and convert it to clean, editable LaTeX code.
  </p>
</div>

<div class="row">
  <div class="col-lg-10 mx-auto">
    <div class="feature-card">
      <h3 class="section-title">Upload Equation Image</h3>




      <form method="POST" enctype="multipart/form-data" id="uploadForm">
        <div class="upload-area" id="dropArea">
          <div class="upload-icon">
            <i class="fas fa-cloud-upload-alt"></i>
          </div>
          <h4 class="upload-text">Drag & Drop your equation image here</h4>
          <p class="upload-hint">or click to browse files (JPG, PNG, GIF, etc.)</p>
          <input type="file" name="image" id="fileInput" accept="image/*" required class="d-none">
        </div>


        
        <div id="uploadMessage" class="upload-message">
          File uploaded successfully!
        </div>

        <div class="text-center" id="uploadButtonContainer" style="display:none; margin-top:15px;">
          <button type="submit" class="btn btn-primary-gradient">
            <i class="fas fa-upload me-2"></i>Upload & Convert
          </button>
        </div>

      </form>
    </div>




    <div class="loading" id="loading">
      <div class="spinner"></div>
      <p>Processing your equation... This may take a few seconds.</p>
    </div>




    <div class="feature-card" id="resultCard" style="display: none;">
      <h3 class="section-title">Generated LaTeX</h3>

      <div class="image-preview-container" id="imagePreviewContainer" style="display: none;">
        <h5>Original Image:</h5>
        <img id="imagePreview" class="image-preview" alt="Uploaded equation">
      </div>

      <div class="rendered-output">
        <h5>Rendered Equation:</h5>
        <div id="renderedContent"></div>
      </div>

      <div class="latex-output" id="latexOutput"></div>

      <div class="solution-output" id="solutionOutput">
        <h5 class="solution-title"><i class="fas fa-calculator me-2"></i>Solution</h5>
        <div class="solution-content" id="solutionContent"></div>
      </div>

      <div class="button-row">
        <button id="solveBtn" class="btn btn-accent-gradient">
          <i class="fas fa-calculator me-2"></i>Solve Equation
        </button>
        <button id="copyBtn" class="btn btn-secondary-gradient">
          <i class="fas fa-copy me-2"></i>Copy LaTeX
        </button>
        <button id="newConversion" class="btn btn-neutral">
          <i class="fas fa-redo me-2"></i>New Conversion
        </button>
        <a href="/" class="btn btn-neutral">
          <i class="fas fa-home me-2"></i>Back to Home
        </a>
      </div>
    </div>
  </div>
</div>
{% endblock %}



{% block extra_js %}
<script>

  // DOM elements

  const dropArea = document.getElementById('dropArea');

  const fileInput = document.getElementById('fileInput');

  const uploadForm = document.getElementById('uploadForm');

  const resultCard = document.getElementById('resultCard');

  const loading = document.getElementById('loading');

  const imagePreview = document.getElementById('imagePreview');

  const imagePreviewContainer = document.getElementById('imagePreviewContainer');

  const solveBtn = document.getElementById('solveBtn');

  const solutionOutput = document.getElementById('solutionOutput');



  const uploadMessage = document.getElementById('uploadMessage');

  const uploadButtonContainer = document.getElementById('uploadButtonContainer');



  // State variables

  let currentLatex = '';



  // File upload handling

  dropArea.addEventListener('click', () => {

    fileInput.click();

  });







  // When user picks file manually

  fileInput.addEventListener('change', () => {

    if (fileInput.files.length) {

      showUploadReadyState(fileInput.files[0].name);

    }

  });



  // When file is dropped

  dropArea.addEventListener('drop', (e) => {

    e.preventDefault();

    dropArea.classList.remove('dragover');



    if (e.dataTransfer.files.length) {

      fileInput.files = e.dataTransfer.files;

      showUploadReadyState(e.dataTransfer.files[0].name);

    }

  });



  // Helper: show "uploaded" message and button

  function showUploadReadyState(fileName) {

    uploadMessage.innerHTML = `✅ <b>${fileName}</b> uploaded successfully!`;

    uploadMessage.style.display = 'block';

    uploadButtonContainer.style.display = 'block';

  }



  // Prevent browser defaults globally

  document.addEventListener('dragover', (e) => e.preventDefault());

  document.addEventListener('drop', (e) => e.preventDefault());



  // Drag and Drop Events

  dropArea.addEventListener('dragover', (e) => {

    e.preventDefault();

    dropArea.classList.add('dragover');

  });



  dropArea.addEventListener('dragleave', () => {

    dropArea.classList.remove('dragover');

  });



  dropArea.addEventListener('drop', (e) => {

    e.preventDefault();

    dropArea.classList.remove('dragover');



    if (e.dataTransfer.files.length) {

      fileInput.files = e.dataTransfer.files;

      uploadForm.requestSubmit(); // 🔥 triggers your async submit code

    }

  });







  // Form submission

  uploadForm.addEventListener('submit', async (e) => {

    e.preventDefault();



    const formData = new FormData(uploadForm);



    try {

      loading.style.display = 'block';

      resultCard.style.display = 'none';

      solutionOutput.style.display = 'none';



      const response = await fetch('/math/process', {

        method: 'POST',

        body: formData

      });



      const data = await response.json();



      if (data.success) {

        currentLatex = data.latex;



        // Display image preview if available

        if (data.image_path) {

          imagePreview.src = '/' + data.image_path;

          imagePreviewContainer.style.display = 'block';

        } else {

          imagePreviewContainer.style.display = 'none';

        }



        // Display results

        document.getElementById('latexOutput').textContent = data.latex;

        document.getElementById('renderedContent').innerHTML = `$$${data.latex}$$`;



        // Render with MathJax if available

        if (typeof MathJax !== 'undefined') {

          MathJax.typesetPromise([document.getElementById('renderedContent')]);

        }



        // Show result card

        resultCard.style.display = 'block';

      } else {

        alert('Error: ' + data.error);

      }

    } catch (error) {

      console.error('Upload error:', error);

      alert('An error occurred during upload. Please try again.');

    } finally {

      loading.style.display = 'none';

    }

  });



  // Solve equation

  solveBtn.addEventListener('click', async () => {

    if (!currentLatex) return;



    try {

      loading.style.display = 'block';



      const response = await fetch('/math/solve', {

        method: 'POST',

        headers: {

          'Content-Type': 'application/json'

        },

        body: JSON.stringify({ latex: currentLatex })

      });



      const data = await response.json();



      if (data.success) {

        const solution = data.solution;

        let solutionText = '';



        if (solution.type === 'equation') {

          solutionText = `Solutions:\n${solution.solutions.join('\n')}`;

        } else if (solution.type === 'expression') {

          solutionText = `Simplified:\n${solution.result}`;

        } else if (solution.type === 'numerical') {

          solutionText = `Numerical value:\n${solution.result}`;

        } else if (solution.type === 'system') {

          solutionText = `System solutions:\n${JSON.stringify(solution.solutions)}`;

        } else if (solution.type === 'error') {

          solutionText = `Error: ${solution.message}`;

        }



        document.getElementById('solutionContent').textContent = solutionText;

        solutionOutput.style.display = 'block';

      } else {

        alert('Error: ' + data.error);

      }

    } catch (error) {

      console.error('Solving error:', error);

      alert('An error occurred while solving the equation. Please try again.');

    } finally {

      loading.style.display = 'none';

    }

  });



  // Copy to clipboard

  document.getElementById('copyBtn').addEventListener('click', () => {

    const latexText = document.getElementById('latexOutput').textContent;

    navigator.clipboard.writeText(latexText).then(() => {

      showNotification('LaTeX code copied to clipboard!');

    }).catch(err => {

      console.error('Failed to copy: ', err);

      alert('Failed to copy LaTeX code. Please try again.');

    });

  });



  // New conversion

  document.getElementById('newConversion').addEventListener('click', () => {

    resultCard.style.display = 'none';

    solutionOutput.style.display = 'none';

    uploadForm.reset();

  });

</script>
{% endblock %}