mirror of
https://github.com/open-webui/open-webui
synced 2025-06-09 07:56:42 +00:00
feat: code format
This commit is contained in:
parent
fb0f106afe
commit
8b1e2ce279
@ -7,6 +7,8 @@ from pydantic import BaseModel
|
|||||||
|
|
||||||
from fpdf import FPDF
|
from fpdf import FPDF
|
||||||
import markdown
|
import markdown
|
||||||
|
import black
|
||||||
|
|
||||||
|
|
||||||
from apps.webui.internal.db import DB
|
from apps.webui.internal.db import DB
|
||||||
from utils.utils import get_admin_user
|
from utils.utils import get_admin_user
|
||||||
@ -26,6 +28,21 @@ async def get_gravatar(
|
|||||||
return get_gravatar_url(email)
|
return get_gravatar_url(email)
|
||||||
|
|
||||||
|
|
||||||
|
class CodeFormatRequest(BaseModel):
|
||||||
|
code: str
|
||||||
|
|
||||||
|
|
||||||
|
@router.post("/code/format")
|
||||||
|
async def format_code(request: CodeFormatRequest):
|
||||||
|
try:
|
||||||
|
formatted_code = black.format_str(request.code, mode=black.Mode())
|
||||||
|
return {"code": formatted_code}
|
||||||
|
except black.NothingChanged:
|
||||||
|
return {"code": request.code}
|
||||||
|
except Exception as e:
|
||||||
|
raise HTTPException(status_code=400, detail=str(e))
|
||||||
|
|
||||||
|
|
||||||
class MarkdownForm(BaseModel):
|
class MarkdownForm(BaseModel):
|
||||||
md: str
|
md: str
|
||||||
|
|
||||||
|
@ -22,6 +22,39 @@ export const getGravatarUrl = async (email: string) => {
|
|||||||
return res;
|
return res;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export const formatPythonCode = async (code: string) => {
|
||||||
|
let error = null;
|
||||||
|
|
||||||
|
const res = await fetch(`${WEBUI_API_BASE_URL}/utils/code/format`, {
|
||||||
|
method: 'POST',
|
||||||
|
headers: {
|
||||||
|
'Content-Type': 'application/json'
|
||||||
|
},
|
||||||
|
body: JSON.stringify({
|
||||||
|
code: code
|
||||||
|
})
|
||||||
|
})
|
||||||
|
.then(async (res) => {
|
||||||
|
if (!res.ok) throw await res.json();
|
||||||
|
return res.json();
|
||||||
|
})
|
||||||
|
.catch((err) => {
|
||||||
|
console.log(err);
|
||||||
|
|
||||||
|
error = err;
|
||||||
|
if (err.detail) {
|
||||||
|
error = err.detail;
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
});
|
||||||
|
|
||||||
|
if (error) {
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
|
||||||
|
return res;
|
||||||
|
};
|
||||||
|
|
||||||
export const downloadChatAsPDF = async (chat: object) => {
|
export const downloadChatAsPDF = async (chat: object) => {
|
||||||
let error = null;
|
let error = null;
|
||||||
|
|
||||||
|
@ -6,10 +6,15 @@
|
|||||||
import { acceptCompletion } from '@codemirror/autocomplete';
|
import { acceptCompletion } from '@codemirror/autocomplete';
|
||||||
import { indentWithTab } from '@codemirror/commands';
|
import { indentWithTab } from '@codemirror/commands';
|
||||||
|
|
||||||
|
import { indentUnit } from '@codemirror/language';
|
||||||
import { python } from '@codemirror/lang-python';
|
import { python } from '@codemirror/lang-python';
|
||||||
import { oneDark } from '@codemirror/theme-one-dark';
|
import { oneDark } from '@codemirror/theme-one-dark';
|
||||||
|
|
||||||
import { onMount } from 'svelte';
|
import { onMount, createEventDispatcher } from 'svelte';
|
||||||
|
import { formatPythonCode } from '$lib/apis/utils';
|
||||||
|
import { toast } from 'svelte-sonner';
|
||||||
|
|
||||||
|
const dispatch = createEventDispatcher();
|
||||||
|
|
||||||
export let boilerplate = '';
|
export let boilerplate = '';
|
||||||
export let value = '';
|
export let value = '';
|
||||||
@ -19,10 +24,31 @@
|
|||||||
let isDarkMode = false;
|
let isDarkMode = false;
|
||||||
let editorTheme = new Compartment();
|
let editorTheme = new Compartment();
|
||||||
|
|
||||||
|
const formatPythonCodeHandler = async () => {
|
||||||
|
if (codeEditor) {
|
||||||
|
console.log('formatPythonCodeHandler');
|
||||||
|
const res = await formatPythonCode(value).catch((error) => {
|
||||||
|
toast.error(error);
|
||||||
|
return null;
|
||||||
|
});
|
||||||
|
|
||||||
|
if (res && res.code) {
|
||||||
|
const formattedCode = res.code;
|
||||||
|
codeEditor.dispatch({
|
||||||
|
changes: [{ from: 0, to: codeEditor.state.doc.length, insert: formattedCode }]
|
||||||
|
});
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
let extensions = [
|
let extensions = [
|
||||||
basicSetup,
|
basicSetup,
|
||||||
keymap.of([{ key: 'Tab', run: acceptCompletion }, indentWithTab]),
|
keymap.of([{ key: 'Tab', run: acceptCompletion }, indentWithTab]),
|
||||||
python(),
|
python(),
|
||||||
|
indentUnit.of(' '),
|
||||||
placeholder('Enter your code here...'),
|
placeholder('Enter your code here...'),
|
||||||
EditorView.updateListener.of((e) => {
|
EditorView.updateListener.of((e) => {
|
||||||
if (e.docChanged) {
|
if (e.docChanged) {
|
||||||
@ -78,8 +104,22 @@
|
|||||||
attributeFilter: ['class']
|
attributeFilter: ['class']
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Add a keyboard shortcut to format the code when Ctrl/Cmd + S is pressed
|
||||||
|
// Override the default browser save functionality
|
||||||
|
|
||||||
|
const handleSave = (e) => {
|
||||||
|
if ((e.ctrlKey || e.metaKey) && e.key === 's') {
|
||||||
|
e.preventDefault();
|
||||||
|
formatPythonCodeHandler();
|
||||||
|
dispatch('save');
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
document.addEventListener('keydown', handleSave);
|
||||||
|
|
||||||
return () => {
|
return () => {
|
||||||
observer.disconnect();
|
observer.disconnect();
|
||||||
|
document.removeEventListener('keydown', handleSave);
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
</script>
|
</script>
|
||||||
|
@ -7,6 +7,8 @@
|
|||||||
# Use Sphinx-style docstrings to document your tools, they will be used for generating tools specifications
|
# Use Sphinx-style docstrings to document your tools, they will be used for generating tools specifications
|
||||||
# Please refer to function_calling_filter_pipeline.py file from pipelines project for an example
|
# Please refer to function_calling_filter_pipeline.py file from pipelines project for an example
|
||||||
|
|
||||||
|
# Tip: Use Ctrl/Cmd + S to format the code
|
||||||
|
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
import requests
|
import requests
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user