Skip to content

Commit cef9e10

Browse files
authored
Merge pull request #12 from dasmy/dev/show_code_in_context
Dev/show code in context
2 parents 31f18e1 + 8aab4cb commit cef9e10

File tree

3 files changed

+24
-22
lines changed

3 files changed

+24
-22
lines changed

frontend/src/App.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,6 @@ function App() {
126126
const data = await response.json();
127127
const code = data.code;
128128

129-
addMessage({ text: code, type: "code", role: "system" });
130129
addMessage({ text: data.text, type: "message", role: "system" });
131130

132131
if (response.status != 200) {

frontend/src/components/Chat.tsx

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -30,26 +30,36 @@ function Message(props: {
3030
</div>
3131
</div>
3232
<div className="message-body">
33-
{props.type == "code" && (
34-
<div>
35-
I generated the following code:
36-
<SyntaxHighlighter wrapLongLines={true} language="python">
37-
{text}
38-
</SyntaxHighlighter>
39-
</div>
40-
)}
41-
4233
{props.type == "message" &&
4334
(props.showLoader ? (
4435
<div>
4536
{text} {props.showLoader ? <div className="loader"></div> : null}
4637
</div>
4738
) : (
4839
isMarkdown(text) ?
49-
<ReactMarkdown
50-
children={text}
51-
remarkPlugins={[remarkGfm]}
52-
/> :
40+
<ReactMarkdown
41+
children={text}
42+
remarkPlugins={[remarkGfm]}
43+
components={{
44+
code({node, inline, className, children, style, ...props}) {
45+
const match = /language-(\w+)/.exec(className || '')
46+
return !inline ? (
47+
<SyntaxHighlighter
48+
{...props}
49+
children={String(children).replace(/\n$/, '')}
50+
wrapLongLines={true}
51+
language={match ? match[1] : "python"}
52+
PreTag="div"
53+
/>
54+
) : (
55+
<code {...props} className={className}>
56+
{children}
57+
</code>
58+
)
59+
}
60+
}}
61+
/>
62+
:
5363
<div className="cell-output" dangerouslySetInnerHTML={{ __html: text }}></div>
5464
))}
5565

gpt_code_ui/webapp/main.py

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -171,14 +171,7 @@ def extract_code(text):
171171
if single_match:
172172
return single_match.group(1).strip()
173173

174-
def extract_non_code(text):
175-
# Replace triple backtick blocks
176-
text = re.sub(r'```(?:\w+\n)?(.+?)```', '', text, flags=re.DOTALL)
177-
# Replace single backtick blocks
178-
text = re.sub(r'`(.+?)`', '', text, flags=re.DOTALL)
179-
return text.strip()
180-
181-
return extract_code(content), extract_non_code(content), 200
174+
return extract_code(content), content.strip(), 200
182175

183176
# We know this Flask app is for local use. So we can disable the verbose Werkzeug logger
184177
log = logging.getLogger('werkzeug')

0 commit comments

Comments
 (0)