File size: 10,603 Bytes
3e6b063
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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
{% extends "base.html" %}

{% block title %}Graph & Plot Generator | 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);

}

  .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 %}