Spaces:
Paused
Paused
| {% extends "base.html" %} | |
| {% block title %}Graph & Plot Generator | TexLab{% endblock %} | |
| {% block extra_css %} | |
| <style> | |
| .feature-card { | |
| display: inline-block; | |
| width: 100%; | |
| height: auto ; | |
| padding: 20px 25px; | |
| margin-bottom: 20px; | |
| background: #fff; | |
| border-radius: 12px; | |
| box-shadow: 0 4px 10px rgba(0, 0, 0, 0.05); | |
| } | |
| .command-input { | |
| border: 2px solid #ddd; | |
| border-radius: 10px; | |
| padding: 15px; | |
| background: white; | |
| box-shadow: 0 5px 15px rgba(0, 0, 0, 0.05); | |
| margin-bottom: 20px; | |
| } | |
| .command-input input { | |
| width: 100%; | |
| padding: 12px 15px; | |
| border: 1px solid #ddd; | |
| border-radius: 8px; | |
| font-size: 1.1rem; | |
| font-family: 'Consolas', 'Courier New', monospace; | |
| } | |
| .command-input input:focus { | |
| outline: none; | |
| border-color: #667eea; | |
| box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.2); | |
| } | |
| .examples { | |
| background: #e3f2fd; | |
| border-radius: 10px; | |
| padding: 20px; | |
| margin: 20px 0; | |
| } | |
| .example-list { | |
| text-align: left; | |
| padding-left: 20px; | |
| } | |
| .example-list li { | |
| margin-bottom: 10px; | |
| font-family: 'Consolas', 'Courier New', monospace; | |
| background: white; | |
| padding: 8px 12px; | |
| border-radius: 5px; | |
| display: inline-block; | |
| margin: 5px; | |
| } | |
| .graph-container { | |
| text-align: center; | |
| margin: 30px 0; | |
| padding: 20px; | |
| background: white; | |
| border-radius: 10px; | |
| box-shadow: 0 5px 15px rgba(0, 0, 0, 0.05); | |
| min-height: 300px; | |
| display: flex; | |
| align-items: center; | |
| justify-content: center; | |
| } | |
| .graph-placeholder { | |
| color: #888; | |
| } | |
| .graph-image { | |
| max-width: 100%; | |
| height: auto; | |
| border-radius: 10px; | |
| box-shadow: 0 5px 15px rgba(0, 0, 0, 0.1); | |
| } | |
| .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); } | |
| } | |
| .function-info { | |
| background: #f8f9fa; | |
| border-radius: 10px; | |
| padding: 15px; | |
| margin: 20px 0; | |
| text-align: center; | |
| font-family: 'Consolas', 'Courier New', monospace; | |
| font-size: 1.1rem; | |
| } | |
| @media (max-width: 768px) { | |
| .command-input input { | |
| font-size: 1rem; | |
| } | |
| .example-list li { | |
| display: block; | |
| margin: 5px 0; | |
| } | |
| } | |
| </style> | |
| {% endblock %} | |
| {% block content %} | |
| <div class="page-header"> | |
| <h1 class="page-title"><i class="fas fa-chart-line me-2"></i>Graph & Plot Generator</h1> | |
| <p class="page-subtitle"> | |
| Generate beautiful mathematical graphs and plots from simple text commands. | |
| </p> | |
| </div> | |
| <div class="row"> | |
| <div class="col-lg-10 mx-auto"> | |
| <div class="feature-card"> | |
| <h3 class="section-title">Create Your Graph</h3> | |
| <div class="command-input"> | |
| <input type="text" id="commandInput" placeholder="Enter a command like: plot x^2 from -5 to 5" autocomplete="off"> | |
| </div> | |
| <div class="text-center"> | |
| <button id="generateBtn" class="btn btn-primary-gradient"> | |
| <i class="fas fa-chart-line me-2"></i>Generate Graph | |
| </button> | |
| <button id="clearBtn" class="btn btn-neutral ms-2"> | |
| <i class="fas fa-eraser me-2"></i>Clear | |
| </button> | |
| </div> | |
| </div> | |
| <div class="examples"> | |
| <h5><i class="fas fa-lightbulb me-2"></i>Example Commands:</h5> | |
| <ul class="example-list"> | |
| <li>plot x^2 from -5 to 5</li> | |
| <li>plot sin(x) from 0 to 2*pi</li> | |
| <li>plot x^3 - 2*x + 1 from -3 to 3</li> | |
| <li>plot cos(x) from -pi to pi</li> | |
| <li>plot sqrt(x) from 0 to 10</li> | |
| <li>plot exp(-x^2) from -3 to 3</li> | |
| </ul> | |
| </div> | |
| <div class="loading" id="loading"> | |
| <div class="spinner"></div> | |
| <p>Generating your graph... This may take a few seconds.</p> | |
| </div> | |
| <div class="function-info" id="functionInfo" style="display: none;"> | |
| Function: <span id="functionText"></span> | Range: <span id="rangeText"></span> | |
| </div> | |
| <div class="graph-container"> | |
| <div id="graphPlaceholder" class="graph-placeholder"> | |
| <i class="fas fa-chart-line fa-3x mb-3"></i> | |
| <h4>Your Graph Will Appear Here</h4> | |
| <p class="text-muted">Enter a plot command above and click "Generate Graph"</p> | |
| </div> | |
| <img id="graphImage" class="graph-image" style="display: none;"> | |
| </div> | |
| <div class="feature-card" id="latexCard" style="display: none;"> | |
| <h3 class="section-title">✅ LaTeX Code</h3> | |
| <div style="margin-bottom: 15px; padding: 15px; background: #e8f5e9; border-radius: 8px; text-align: left;"> | |
| <strong>Latex code will appear here</strong> | |
| <ul style="margin: 10px 0; padding-left: 20px;"> | |
| </ul> | |
| </div> | |
| <!-- <div class="latex-output" id="latexOutput" style="background: #f8f9fa; border-radius: 10px; padding: 20px; border: 1px solid #eee; font-family: 'Consolas', 'Courier New', monospace; font-size: 1rem; white-space: pre-wrap; word-break: break-all; min-height: 100px; margin: 20px 0; max-height: 400px; overflow-y: auto;"></div> --> | |
| <div class="latex-output" id="latexOutput" style="display: none;"></div> | |
| <div class="actions" style="margin-top: 20px; text-align: center;"> | |
| <button id="copyLatexBtn" class="btn btn-secondary-gradient"> | |
| <i class="fas fa-copy me-2"></i>Copy LaTeX | |
| </button> | |
| <button id="downloadLatexBtn" class="btn btn-success-gradient" style="background: linear-gradient(135deg, #28a745 0%, #20c997 100%); color: white; border: none; padding: 12px 25px; border-radius: 50px; font-weight: 600; transition: all 0.3s ease;"> | |
| <i class="fas fa-download me-2"></i>Download .tex File | |
| </button> | |
| <a href="/graph" class="btn btn-neutral"> | |
| <i class="fas fa-redo me-2"></i>New Graph | |
| </a> | |
| <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 commandInput = document.getElementById('commandInput'); | |
| const generateBtn = document.getElementById('generateBtn'); | |
| const clearBtn = document.getElementById('clearBtn'); | |
| const graphPlaceholder = document.getElementById('graphPlaceholder'); | |
| const graphImage = document.getElementById('graphImage'); | |
| const loading = document.getElementById('loading'); | |
| const functionInfo = document.getElementById('functionInfo'); | |
| const functionText = document.getElementById('functionText'); | |
| const rangeText = document.getElementById('rangeText'); | |
| const latexCard = document.getElementById('latexCard'); | |
| const latexOutput = document.getElementById('latexOutput'); | |
| // Focus on input when page loads | |
| window.addEventListener('load', () => { | |
| commandInput.focus(); | |
| }); | |
| // Generate graph on button click | |
| generateBtn.addEventListener('click', generateGraph); | |
| // Generate graph on Enter key | |
| commandInput.addEventListener('keypress', (e) => { | |
| if (e.key === 'Enter') { | |
| generateGraph(); | |
| } | |
| }); | |
| // Clear button | |
| clearBtn.addEventListener('click', () => { | |
| commandInput.value = ''; | |
| graphPlaceholder.style.display = 'block'; | |
| graphImage.style.display = 'none'; | |
| functionInfo.style.display = 'none'; | |
| latexCard.style.display = 'none'; | |
| commandInput.focus(); | |
| }); | |
| // Generate graph function | |
| async function generateGraph() { | |
| const command = commandInput.value.trim(); | |
| if (!command) { | |
| alert('Please enter a command'); | |
| return; | |
| } | |
| try { | |
| // Show loading indicator | |
| loading.style.display = 'block'; | |
| graphPlaceholder.style.display = 'none'; | |
| graphImage.style.display = 'none'; | |
| functionInfo.style.display = 'none'; | |
| latexCard.style.display = 'none'; | |
| // Send request to backend | |
| const response = await fetch('/graph/generate', { | |
| method: 'POST', | |
| headers: { | |
| 'Content-Type': 'application/json' | |
| }, | |
| body: JSON.stringify({ command: command }) | |
| }); | |
| const data = await response.json(); | |
| if (data.success) { | |
| // Display graph | |
| graphImage.src = 'data:image/png;base64,' + data.image; | |
| graphImage.style.display = 'block'; | |
| // Display function info | |
| functionText.textContent = data.function; | |
| rangeText.textContent = data.range; | |
| functionInfo.style.display = 'block'; | |
| // Use real TikZ/pgfplots LaTeX code from backend | |
| latexOutput.textContent = data.latex; | |
| latexCard.style.display = 'block'; | |
| } else { | |
| alert('Error: ' + data.error); | |
| graphPlaceholder.style.display = 'block'; | |
| } | |
| } catch (error) { | |
| console.error('Error:', error); | |
| alert('An error occurred. Please try again.'); | |
| graphPlaceholder.style.display = 'block'; | |
| } finally { | |
| // Hide loading indicator | |
| loading.style.display = 'none'; | |
| } | |
| } | |
| // Copy LaTeX to clipboard | |
| document.getElementById('copyLatexBtn').addEventListener('click', () => { | |
| const latexText = latexOutput.textContent; | |
| navigator.clipboard.writeText(latexText).then(() => { | |
| alert('✅ TikZ/pgfplots LaTeX code copied to clipboard!\nReady to paste into Overleaf.'); | |
| }).catch(err => { | |
| console.error('Failed to copy: ', err); | |
| alert('Failed to copy LaTeX code. Please try again.'); | |
| }); | |
| }); | |
| // Download LaTeX file | |
| document.getElementById('downloadLatexBtn').addEventListener('click', () => { | |
| const latexText = latexOutput.textContent; | |
| const blob = new Blob([latexText], { type: 'text/plain' }); | |
| const url = URL.createObjectURL(blob); | |
| const a = document.createElement('a'); | |
| a.href = url; | |
| a.download = 'graph.tex'; | |
| document.body.appendChild(a); | |
| a.click(); | |
| document.body.removeChild(a); | |
| URL.revokeObjectURL(url); | |
| alert('✅ LaTeX file downloaded!\nYou can now upload it to Overleaf.'); | |
| }); | |
| </script> | |
| {% endblock %} |