Update pages/reader.py
Browse files- pages/reader.py +119 -0
pages/reader.py
CHANGED
|
@@ -2,6 +2,12 @@
|
|
| 2 |
import streamlit as st
|
| 3 |
import pandas as pd
|
| 4 |
import html
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
|
| 7 |
#===config===
|
|
@@ -27,6 +33,111 @@ df = connect_gsheet()
|
|
| 27 |
def format_html(text):
|
| 28 |
return html.escape(text).replace("\n", "<br>")
|
| 29 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
st.title('TXTperpus: Reader')
|
| 31 |
|
| 32 |
paramx = st.query_params["art"]
|
|
@@ -48,6 +159,14 @@ if selection == "Baca Artikel":
|
|
| 48 |
|
| 49 |
fontsize = st.number_input("Ukuran kata", min_value=5, value=16, step=1)
|
| 50 |
no_columns = st.number_input("Jumlah kolom", min_value=1, max_value=4, value=1, step=1)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
|
| 52 |
if not filtered_df.empty:
|
| 53 |
filtered_df = filtered_df.replace("NaN", "Tidak tersedia")
|
|
|
|
| 2 |
import streamlit as st
|
| 3 |
import pandas as pd
|
| 4 |
import html
|
| 5 |
+
from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer, Frame, PageTemplate
|
| 6 |
+
from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle
|
| 7 |
+
from reportlab.lib.pagesizes import A4
|
| 8 |
+
from reportlab.lib.units import cm
|
| 9 |
+
from reportlab.lib.enums import TA_CENTER, TA_JUSTIFY
|
| 10 |
+
import io
|
| 11 |
|
| 12 |
|
| 13 |
#===config===
|
|
|
|
| 33 |
def format_html(text):
|
| 34 |
return html.escape(text).replace("\n", "<br>")
|
| 35 |
|
| 36 |
+
|
| 37 |
+
def create_pdf():
|
| 38 |
+
buffer = io.BytesIO()
|
| 39 |
+
|
| 40 |
+
# Document template
|
| 41 |
+
doc = SimpleDocTemplate(
|
| 42 |
+
buffer,
|
| 43 |
+
pagesize=A4,
|
| 44 |
+
rightMargin=2*cm, leftMargin=2*cm,
|
| 45 |
+
topMargin=2*cm, bottomMargin=2*cm
|
| 46 |
+
)
|
| 47 |
+
|
| 48 |
+
# Create styles
|
| 49 |
+
styles = getSampleStyleSheet()
|
| 50 |
+
styles.add(ParagraphStyle(
|
| 51 |
+
name="ArticleTitle",
|
| 52 |
+
alignment=TA_CENTER,
|
| 53 |
+
fontSize=28,
|
| 54 |
+
leading=32,
|
| 55 |
+
spaceAfter=6,
|
| 56 |
+
textColor="#222",
|
| 57 |
+
fontName="Times-Bold"
|
| 58 |
+
))
|
| 59 |
+
styles.add(ParagraphStyle(
|
| 60 |
+
name="Author",
|
| 61 |
+
alignment=TA_CENTER,
|
| 62 |
+
fontSize=14,
|
| 63 |
+
leading=18,
|
| 64 |
+
textColor="#666",
|
| 65 |
+
italic=True,
|
| 66 |
+
spaceAfter=12
|
| 67 |
+
))
|
| 68 |
+
styles.add(ParagraphStyle(
|
| 69 |
+
name="SectionTitle",
|
| 70 |
+
alignment=TA_CENTER,
|
| 71 |
+
fontSize=18,
|
| 72 |
+
leading=22,
|
| 73 |
+
spaceBefore=16,
|
| 74 |
+
spaceAfter=8,
|
| 75 |
+
textColor="#004488",
|
| 76 |
+
fontName="Times-Bold"
|
| 77 |
+
))
|
| 78 |
+
styles.add(ParagraphStyle(
|
| 79 |
+
name="Meta",
|
| 80 |
+
alignment=TA_CENTER,
|
| 81 |
+
fontSize=fontsize,
|
| 82 |
+
leading=18,
|
| 83 |
+
italic=True,
|
| 84 |
+
textColor="#333",
|
| 85 |
+
spaceAfter=10
|
| 86 |
+
))
|
| 87 |
+
styles.add(ParagraphStyle(
|
| 88 |
+
name="Content",
|
| 89 |
+
alignment=TA_JUSTIFY,
|
| 90 |
+
fontSize=fontsize,
|
| 91 |
+
leading=18,
|
| 92 |
+
textColor="#333"
|
| 93 |
+
))
|
| 94 |
+
|
| 95 |
+
# Elements list
|
| 96 |
+
elements = []
|
| 97 |
+
|
| 98 |
+
# Title + Authors
|
| 99 |
+
elements.append(Paragraph(title, styles["ArticleTitle"]))
|
| 100 |
+
elements.append(Paragraph(f"By {author}", styles["Author"]))
|
| 101 |
+
elements.append(Paragraph(affiliation, styles["Author"]))
|
| 102 |
+
|
| 103 |
+
# Abstracts
|
| 104 |
+
elements.append(Paragraph("Abstract", styles["SectionTitle"]))
|
| 105 |
+
elements.append(Paragraph(abstracten, styles["Meta"]))
|
| 106 |
+
|
| 107 |
+
elements.append(Paragraph("Abstrak", styles["SectionTitle"]))
|
| 108 |
+
elements.append(Paragraph(abstractid, styles["Meta"]))
|
| 109 |
+
elements.append(Paragraph(f"Keywords: {keywords}", styles["Author"]))
|
| 110 |
+
|
| 111 |
+
# Sections
|
| 112 |
+
sections = [
|
| 113 |
+
("Introduction", intro),
|
| 114 |
+
("Method", method),
|
| 115 |
+
("Result", result),
|
| 116 |
+
("Discussion", discussion),
|
| 117 |
+
("Result & Discussion", rnd),
|
| 118 |
+
("Conclusion", conclusion),
|
| 119 |
+
]
|
| 120 |
+
|
| 121 |
+
for sec_title, sec_content in sections:
|
| 122 |
+
elements.append(Paragraph(sec_title, styles["SectionTitle"]))
|
| 123 |
+
elements.append(Paragraph(sec_content, styles["Content"]))
|
| 124 |
+
|
| 125 |
+
# Multi-column layout
|
| 126 |
+
frame_width = (A4[0] - 4*cm) / no_columns
|
| 127 |
+
frames = [
|
| 128 |
+
Frame(2*cm + i*frame_width, 2*cm, frame_width, A4[1]-4*cm, leftPadding=0, rightPadding=0)
|
| 129 |
+
for i in range(no_columns)
|
| 130 |
+
]
|
| 131 |
+
template = PageTemplate(frames=frames)
|
| 132 |
+
doc.addPageTemplates([template])
|
| 133 |
+
|
| 134 |
+
# Build PDF
|
| 135 |
+
doc.build(elements)
|
| 136 |
+
buffer.seek(0)
|
| 137 |
+
return buffer
|
| 138 |
+
|
| 139 |
+
|
| 140 |
+
|
| 141 |
st.title('TXTperpus: Reader')
|
| 142 |
|
| 143 |
paramx = st.query_params["art"]
|
|
|
|
| 159 |
|
| 160 |
fontsize = st.number_input("Ukuran kata", min_value=5, value=16, step=1)
|
| 161 |
no_columns = st.number_input("Jumlah kolom", min_value=1, max_value=4, value=1, step=1)
|
| 162 |
+
|
| 163 |
+
pdf_buffer = create_pdf()
|
| 164 |
+
st.download_button(
|
| 165 |
+
label="📄 Download as PDF",
|
| 166 |
+
data=pdf_buffer,
|
| 167 |
+
file_name="article.pdf",
|
| 168 |
+
mime="application/pdf"
|
| 169 |
+
)
|
| 170 |
|
| 171 |
if not filtered_df.empty:
|
| 172 |
filtered_df = filtered_df.replace("NaN", "Tidak tersedia")
|