panel_example7 / app2.py
EasySci's picture
Update app2.py
3baa447
import panel as pn
pn.extension('katex')
import pytz
from datetime import date
from datetime import datetime
from dateutil.relativedelta import relativedelta, MO
import param
import TLDR
from src.search import Search_Papers
class MainBody(param.Parameterized):
main_body = param.List(default=[pn.pane.LaTeX("Please select some tags!", styles={'font-size': '20pt' , 'font-weight':'bold'})])
@param.depends("main_body")
def update(self):
return pn.Column(*self.main_body)
def add_panel_to_main_body(self, panel):
self.main_body.append(panel)
main_body_instance = MainBody()
paper_list = [] # Add this line to initialize paper_list as an empty list
main_body = [pn.pane.LaTeX("Please select some tags!", styles={'font-size': '20pt'})]
# Create buttons for the toggleable sidebar
# button1 = pn.widgets.Button(icon='home', name="Daily papers", icon_size='1.5em', button_type = 'primary', button_style = 'outline', width = 140, align='center' )
# button2 = pn.widgets.Button(icon='calendar-filled', name="Search papers", icon_size='1.5em', button_type = 'primary', button_style = 'outline', width = 140, align='center')
# button3 = pn.widgets.Button(icon='file-analytics', name='My papers', icon_size='1.5em', button_type = 'primary', button_style = 'outline', width = 140, align='center')
# button4 = pn.widgets.Toggle(name='Accessibility',icon='settings', icon_size='1.5em', button_type='primary', button_style='outline', width=140, align='center', value=False)
button1 = pn.widgets.Button(name="πŸ“‹ Daily papers", button_type = 'default', button_style = 'outline', width = 140, align='center' )
button2 = pn.widgets.Button(name="πŸ“† Search papers", button_type = 'default', button_style = 'outline', width = 140, align='center')
button3 = pn.widgets.Button(name='πŸ“ My papers', button_type = 'default', button_style = 'outline', width = 140, align='center')
button4 = pn.widgets.Toggle(name='βš™οΈ Accessibility', button_type='default', button_style='outline', width=140, align='center', value=False)
# Custom RadioButtonGroup widget
select = pn.widgets.RadioButtonGroup(value="Scientic", options=["General", "Scientic"], name='String', align='center', button_type='default') # , align='center'
select2 = pn.widgets.RadioButtonGroup(value="Normal", options=["Bionic", "Normal"], name='String', align='center', button_type='default')
# Define a callback to display the selected value
def display_selected_value(event):
print(f"Selected value: {event.new}", flush=True)
# Attach the callback to the value_change event of the RadioButtonGroup
select.param.watch(display_selected_value, 'value')
select2.param.watch(display_selected_value, 'value')
# Create a column layout for the buttons inside the toggleable sidebar
buttons = pn.Column(
button1, button2, button3, button4,
pn.Column(
select,
select2,
visible=False,
sizing_mode='stretch_width',
),
css_classes=['hidden']
)
# Define a callback to show/hide the select buttons when "Settings" button is toggled
def toggle_settings(event):
buttons[-1].visible = event.new
button4.param.watch(toggle_settings, 'value')
# Dictionary to store the previous content of main_panel and main_panel_button3
previous_content = {}
current_state = 'main_panel' # Variable to keep track of the current state
# Function to reset the main panel content to the initial content and store the previous content
def reset_main_panel(event):
global previous_content, current_state
# Store the previous content of the current state if it's not already stored
previous_content[current_state] = main_body_instance.main_body.copy()
# Update the current state to mail_panel
current_state = 'main_panel'
# Reset the main panel to the initial state
main_body_instance.main_body.clear() # Clear the existing main panel content
# Function to update the main panel content when button3 is pressed
def update_main_panel_button3(event):
global previous_content, current_state
# Store the previous content of the current state if it's not already stored
previous_content[current_state] = main_body_instance.main_body.copy()
# Update the current state to main_panel_button3
current_state = 'main_panel_button3'
# Reset the main panel to the desired content when button3 is pressed
main_body_instance.main_body.clear() # Clear the existing main panel content
# Attach the functions to the on_click event of button1 and button3
button1.on_click(reset_main_panel)
button3.on_click(update_main_panel_button3)
# Function to check if there's previous content and display it, otherwise show a default message
def show_previous_content(event):
global previous_content, current_state
# Check if there's previous content for the current state
print(current_state, flush = True)
if current_state in previous_content:
main_body_instance.main_body.clear() # Clear the existing main panel content
main_body_instance.main_body.extend(previous_content[current_state]) # Add the previous content
else:
main_body_instance.main_body.clear() # Clear the existing main panel content
main_body_instance.main_body.append(pn.pane.Markdown("### No previous content available!"))
main_body_instance.param.trigger('main_body') # Trigger the update manually
# Attach the show_previous_content function to the on_click event of button1 and button3
button1.on_click(show_previous_content)
button3.on_click(show_previous_content)
# List to store the entered options
loaded_dict = TLDR.load_categories()
arxiv_tags = list(loaded_dict.keys())
entered_options = list(loaded_dict.values())
# Create buttons for the header
header_buttons = pn.Row(sizing_mode='stretch_width', css_classes=['header-buttons'])
# "+" button to trigger the addition to the header
add_to_header_button = pn.widgets.Button(name="", icon='search', icon_size='1.5em', button_style = 'outline',button_type = 'light')
# List to store the names of buttons to be added to the header
buttons_to_add = []
paper_list = []
# Callback for adding selected options to the list
def add_to_header(event):
global paper_list
selected_options = filter_list.value
if selected_options:
for option in selected_options:
if option not in buttons_to_add: # Check if option is already in header
buttons_to_add.append(option) # Add to header if not already present
paper_list_itr = TLDR.run_code(option, loaded_dict)
paper_list.append(paper_list_itr)
filter_list.value = [] # Clear the selected options after adding them to the header
update_header() # Update the header after adding options
add_to_header_button.on_click(add_to_header)
# Function to update the header layout with the newly created buttons
def update_header():
global paper_list
header_buttons.clear() # Clear the existing buttons
for button_name in buttons_to_add:
header_button = pn.widgets.Button(name=button_name, button_type = 'primary', button_style = 'outline')
header_button.on_click(remove_from_header) # Add callback to remove the header button
header_buttons.append(header_button)
buttons_to_add
# Update the filter list options to exclude buttons that are already in the header
filter_list.options = [option for option in entered_options if option not in buttons_to_add]
main_body_instance.main_body = TLDR.update_mainTLDR(buttons_to_add, paper_list)
# Callback to remove the clicked header button
def remove_from_header(event):
global paper_list
button = event.obj # Get the clicked button
if button.name in buttons_to_add:
del paper_list[buttons_to_add.index(button.name)]
buttons_to_add.remove(button.name) # Remove from the header buttons list
filter_list.options.append(button.name) # Add back to the filter list options
update_header() # Update the header and filter list
# MultiChoice widget to display the filter options with delete buttons
filter_list = pn.widgets.MultiChoice(
name='',
value=[],
options=entered_options,
margin=(20, 10),
sizing_mode='fixed',
solid=False,
styles={'background': '#f0f0f0'},
placeholder="Search Topics"
)
# Layout using Template
template = pn.template.FastListTemplate(
title="EasySciRead",
header=[pn.Row(header_buttons, width=750, sizing_mode='stretch_width'), pn.Row(filter_list, width=250), pn.Row(add_to_header_button, width=55)],
main= main_body_instance.update,
sidebar=[buttons],
accent_base_color="#88d8b0",
header_background="#FFFFFF",
header_color="#000000",
text_align='center',
sidebar_width=160,
sizing_mode = 'stretch_both',
header_links=True, # Add this line to enable smooth scrolling
)
# Run the app
template.servable()