Markdown Viewer

import os
import streamlit as st

Print banner.

@st.cache_data
def print_banner():
  print("""
                 .___              .__
      _____    __| _/        ___  _|__| ______  _  __
     /     \\  / __ |  ______ \\  \\/ /  |/ __ \\ \\/ \\/ /
    |  Y Y  \\/ /_/ | /_____/  \\   /|  \\  ___/\\     /
    |__|_|  /\\____ |           \\_/ |__|\\___  >\\/\\_/
          \\/      \\/                       \\/

  """)
  return 1

print_banner()

Find all files in the current directory that have a .md extension. These files might contain text generated by large language models. If a particular .md file is a response to a specific question, that question will be stored in a separate file with the same base name but a .q.md extension. Do not include any .q.md files in the final list of files.

md_files = [f for f in os.listdir('.') if f.endswith('.md') and not f.endswith('.q.md')]

Sort files based on their modification time

md_files.sort(key=os.path.getmtime, reverse=True)

Create radio buttons to select a file

selected_file = st.sidebar.radio("Select markdown file:", md_files)

Adds a prefix to each line of a multi-line string.

def prefix_lines(query, prefix):
    lines = query.splitlines()
    prefixed_lines = [prefix + line for line in lines]
    return "\n".join(prefixed_lines)

Check if query file exists.

query_file_path = selected_file[:-3] + ".q.md"
if os.path.exists(query_file_path):
    with open(query_file_path, 'r', encoding='utf-8') as file:
        query = file.read()
    st.write(prefix_lines(query, "> "))
    st.write("---");

Read the contents of selected file

with open(selected_file, 'r', encoding='utf-8') as file:
    md_text = file.read()

Display the contents of the file that has been selected.

st.write(md_text)