Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -639,29 +639,60 @@ def edit_file(file):
|
|
| 639 |
content = f.read()
|
| 640 |
new_content = st.text_area("Edit HTML content", content, height=400)
|
| 641 |
if st.button("Save HTML"):
|
| 642 |
-
|
| 643 |
-
|
| 644 |
-
st.success("File updated successfully!")
|
| 645 |
else: # For .md and other text files
|
| 646 |
with open(file, 'r', encoding='utf-8') as f:
|
| 647 |
content = f.read()
|
| 648 |
new_content = st.text_area("Edit content", content, height=400)
|
| 649 |
if st.button("Save"):
|
| 650 |
-
|
| 651 |
-
|
| 652 |
-
st.success("File updated successfully!")
|
| 653 |
except Exception as e:
|
| 654 |
st.error(f"Error editing {file}: {str(e)}")
|
| 655 |
-
|
| 656 |
def display_pdf(file_path):
|
| 657 |
try:
|
| 658 |
with open(file_path, "rb") as f:
|
| 659 |
-
|
| 660 |
-
|
| 661 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 662 |
except Exception as e:
|
| 663 |
st.error(f"Error displaying PDF {file_path}: {str(e)}")
|
| 664 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 665 |
def display_html(file_path):
|
| 666 |
try:
|
| 667 |
with open(file_path, 'r', encoding='utf-8') as f:
|
|
|
|
| 639 |
content = f.read()
|
| 640 |
new_content = st.text_area("Edit HTML content", content, height=400)
|
| 641 |
if st.button("Save HTML"):
|
| 642 |
+
new_file_path = update_file_with_timestamp(file, new_content)
|
| 643 |
+
st.success(f"File updated successfully! New file: {new_file_path}")
|
|
|
|
| 644 |
else: # For .md and other text files
|
| 645 |
with open(file, 'r', encoding='utf-8') as f:
|
| 646 |
content = f.read()
|
| 647 |
new_content = st.text_area("Edit content", content, height=400)
|
| 648 |
if st.button("Save"):
|
| 649 |
+
new_file_path = update_file_with_timestamp(file, new_content)
|
| 650 |
+
st.success(f"File updated successfully! New file: {new_file_path}")
|
|
|
|
| 651 |
except Exception as e:
|
| 652 |
st.error(f"Error editing {file}: {str(e)}")
|
| 653 |
+
|
| 654 |
def display_pdf(file_path):
|
| 655 |
try:
|
| 656 |
with open(file_path, "rb") as f:
|
| 657 |
+
pdf_reader = PyPDF2.PdfReader(f)
|
| 658 |
+
num_pages = len(pdf_reader.pages)
|
| 659 |
+
|
| 660 |
+
st.write(f"Total Pages: {num_pages}")
|
| 661 |
+
|
| 662 |
+
for page_num in range(num_pages):
|
| 663 |
+
page = pdf_reader.pages[page_num]
|
| 664 |
+
st.subheader(f"Page {page_num + 1}")
|
| 665 |
+
st.write(page.extract_text())
|
| 666 |
+
|
| 667 |
+
if page_num < num_pages - 1:
|
| 668 |
+
st.markdown("---") # Add a separator between pages
|
| 669 |
except Exception as e:
|
| 670 |
st.error(f"Error displaying PDF {file_path}: {str(e)}")
|
| 671 |
|
| 672 |
+
|
| 673 |
+
|
| 674 |
+
def save_file_with_timestamp(file_path, content, file_type):
|
| 675 |
+
directory, filename = os.path.split(file_path)
|
| 676 |
+
name, ext = os.path.splitext(filename)
|
| 677 |
+
timestamp = datetime.datetime.now().strftime("%m%d_%H%M")
|
| 678 |
+
new_filename = f"{timestamp}_{name}{ext}"
|
| 679 |
+
new_file_path = os.path.join(directory, new_filename)
|
| 680 |
+
|
| 681 |
+
if file_type == 'pdf':
|
| 682 |
+
with open(new_file_path, 'wb') as f:
|
| 683 |
+
f.write(content)
|
| 684 |
+
else: # For HTML and other text files
|
| 685 |
+
with open(new_file_path, 'w', encoding='utf-8') as f:
|
| 686 |
+
f.write(content)
|
| 687 |
+
|
| 688 |
+
return new_file_path
|
| 689 |
+
|
| 690 |
+
def update_file_with_timestamp(file_path, new_content):
|
| 691 |
+
file_type = os.path.splitext(file_path)[1].lower()[1:] # Get file extension without the dot
|
| 692 |
+
new_file_path = save_file_with_timestamp(file_path, new_content, file_type)
|
| 693 |
+
os.remove(file_path) # Remove the old file
|
| 694 |
+
return new_file_path
|
| 695 |
+
|
| 696 |
def display_html(file_path):
|
| 697 |
try:
|
| 698 |
with open(file_path, 'r', encoding='utf-8') as f:
|