Skip to content

Commit

Permalink
Merge pull request #1 from haesleinhuepf/hide_code
Browse files Browse the repository at this point in the history
make code hidden with option to show
  • Loading branch information
tischi authored Sep 14, 2023
2 parents 66d23f4 + 9457e64 commit 1354c4d
Showing 1 changed file with 36 additions and 2 deletions.
38 changes: 36 additions & 2 deletions src/bia_tischi/_utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,14 +91,13 @@ def generate_and_execute_code(task: str):
The code should do the following:
"""
from ._utilities import generate_code
from IPython.display import display, Markdown

if _context.verbose:
print("Code request:\n", additional_hints + task + "\n")

code, full_response = generate_code(additional_hints + task)

display(Markdown(full_response))
output(full_response)

if _context.verbose:
print("Code response:\n", code)
Expand All @@ -114,6 +113,41 @@ def generate_and_execute_code(task: str):
return "Code was generated and executed."


def output(message):
"""Display content in the notebook."""
message = message.replace("```python", "```")

sub_messages = message.split("```")

if len(sub_messages) == 1:
output_code(sub_messages[0])
else:
for i, m in enumerate(sub_messages):
if i % 2 == 0:
output_text(m)
else:
output_code(m)

def output_text(markdown):
"""Display markdown content in the notebook."""
from IPython.display import display, Markdown
display(Markdown(markdown))

def output_code(code):
"""Display code content in the notebook."""
from IPython.display import display, Markdown
from IPython.core.display import HTML

display(HTML(f"""
<details>
<summary> Show code </summary>
<pre>
{code}
</pre>
</details>
"""))


def generate_code(task):
"""Uses a language model to generate code that solves a given task."""
full_response = prompt(task)
Expand Down

0 comments on commit 1354c4d

Please sign in to comment.