Skip to content

Instantly share code, notes, and snippets.

@mpottinger
Created July 3, 2024 20:41
Show Gist options
  • Select an option

  • Save mpottinger/d992ec2851eeb4cfbdc66fc95de288d1 to your computer and use it in GitHub Desktop.

Select an option

Save mpottinger/d992ec2851eeb4cfbdc66fc95de288d1 to your computer and use it in GitHub Desktop.
import json
from flask import Flask, request, render_template_string
app = Flask(__name__)
HTML = '''
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>LLM Code Helper</title>
<script src="https://unpkg.com/htmx.org@1.9.10"></script>
<style>
body { font-family: Arial, sans-serif; line-height: 1.6; padding: 20px; }
textarea { width: 100%; height: 200px; margin-bottom: 10px; }
button { padding: 10px; cursor: pointer; }
pre { background-color: #f4f4f4; padding: 10px; white-space: pre-wrap; word-wrap: break-word; }
</style>
</head>
<body>
<h1>LLM Code Helper</h1>
<h2>Format Code</h2>
<form hx-post="/format-code" hx-target="#formatted-code">
<textarea name="code" placeholder="Paste your code here"></textarea>
<button type="submit">Format Code</button>
</form>
<div id="formatted-code"></div>
<h2>Process Changes</h2>
<form hx-post="/process-changes" hx-target="#processed-code">
<textarea name="original_code" placeholder="Paste your original code here"></textarea>
<textarea name="changes" placeholder="Paste your LLM provided JSON changes here"></textarea>
<button type="submit">Process Changes</button>
</form>
<div id="processed-code"></div>
<script>
function copyToClipboard(elementId) {
const el = document.getElementById(elementId);
navigator.clipboard.writeText(el.textContent).then(() => {
alert('Copied to clipboard!');
});
}
</script>
</body>
</html>
'''
@app.route('/')
def index():
return render_template_string(HTML)
@app.route('/format-code', methods=['POST'])
def format_code():
code = request.form['code']
formatted_code = '\n'.join(f"{i + 1}. {line}" for i, line in enumerate(code.split('\n')))
return f'''
<h3>Formatted Code:</h3>
<button onclick="copyToClipboard('formatted-code-pre')">Copy Code</button>
<pre id="formatted-code-pre">{formatted_code}</pre>
'''
@app.route('/process-changes', methods=['POST'])
def process_changes():
original_code = request.form['original_code']
changes = json.loads(request.form['changes'])
lines = original_code.split('\n')
for change in reversed(changes):
start, end = map(int, change['lines'].split('-')) if '-' in change['lines'] else (
int(change['lines']), int(change['lines']))
if change['type'] == 'remove':
del lines[start - 1:end]
elif change['type'] == 'insertafter':
lines.insert(start, change['text'])
elif change['type'] == 'replace':
lines[start - 1:end] = [change['text']]
processed_code = '\n'.join(lines)
return f'''
<h3>Processed Code:</h3>
<button onclick="copyToClipboard('processed-code-pre')">Copy Code</button>
<pre id="processed-code-pre">{processed_code}</pre>
'''
if __name__ == '__main__':
app.run(debug=True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment