working on 3d cards

This commit is contained in:
2025-08-06 16:26:26 +02:00
parent 7c24d9a7c5
commit 6872056901
3 changed files with 267 additions and 27 deletions

View File

@ -3,12 +3,13 @@ import base64
from io import BytesIO
from hipsterfy.hipsterfy import Hipsterfy, HipsterfyPlaylist, HipsterfyPlaylistItem, CardStyle
import qrcode
import traceback
import subprocess
pn.extension("filedownload", "notifications", "location")
def create_panel_page(hipsterfy: Hipsterfy, playlist_uri: str=None, root_url = None) -> pn.Template:
# create widgets
@ -18,6 +19,23 @@ def create_panel_page(hipsterfy: Hipsterfy, playlist_uri: str=None, root_url = N
additional_items = pn.widgets.MultiChoice(name='Additional Items', options=HipsterfyPlaylistItem.get_available_properties(), value=["artists", "album", "popularity"])
card_style = pn.widgets.Select(name='Card Style', options=[style.value for style in CardStyle], value=CardStyle.FULL_ALBUM_COVER.value)
cards_per_row_widget = pn.widgets.IntSlider(name="Cards per Row in print", start=1, end=8, value=4, step=1)
mode_selector = pn.widgets.RadioButtonGroup(
name="Card Generation Mode (2D for printing or 3D to produce stl files for 3D printing)",
options=["2D", "3D"],
value="2D",
button_type="primary",
button_style="outline",
sizing_mode='stretch_width'
)
# add a callback that hides the card style widget if 3D mode is selected
def toggle_card_style_visibility(event):
if mode_selector.value == "3D":
card_style.visible = False
else:
card_style.visible = True
mode_selector.param.watch(toggle_card_style_visibility, 'value')
pn.state.location.sync(playlist_uri, {"value": "playlist_uri"})
pn.state.location.sync(primary_item, {"value": "primary_item"})
@ -25,6 +43,7 @@ def create_panel_page(hipsterfy: Hipsterfy, playlist_uri: str=None, root_url = N
pn.state.location.sync(additional_items, {"value": "additional_items"})
pn.state.location.sync(card_style, {"value": "card_style"})
pn.state.location.sync(cards_per_row_widget, {"value": "cards_per_row"})
pn.state.location.sync(mode_selector, {"value": "mode_selector"})
create_preview_button = pn.widgets.Button(name='Parse Playlist and Generate Cards', button_type='primary', sizing_mode='stretch_width')
@ -110,6 +129,9 @@ def create_panel_page(hipsterfy: Hipsterfy, playlist_uri: str=None, root_url = N
download_back_html_button.callback = lambda: BytesIO(generate_html(cards_back, row_reverse=True))
download_back_html_button.visible = True
download_front_html_button.filename = f"hipsterfy_front_cards.html"
download_back_html_button.filename = f"hipsterfy_back_cards.html"
print_instructions.visible = True # Show print instructions after generating preview
pn.state.notifications.success("Download buttons updated successfully!. Download and print them from the sidebar")
@ -121,47 +143,77 @@ def create_panel_page(hipsterfy: Hipsterfy, playlist_uri: str=None, root_url = N
# clear previous cards
front_cards_column.clear()
back_cards_column.clear()
front_cards_column.loading = True
back_cards_column.loading = True
front_cards_column.append(pn.pane.HTML("<h2>Front Cards</h2>", sizing_mode='stretch_width'))
back_cards_column.append(pn.pane.HTML("<h2>Back Cards</h2>", sizing_mode='stretch_width'))
# get playlist URI
uri = playlist_uri.value.strip()
if not uri:
pn.state.notifications.error("Please enter a valid Spotify Playlist URI.")
return
# create HipsterfyPlaylist instance
hipsterfy_playlist = HipsterfyPlaylist(uri, hipsterfy)
new_back_cards = []
new_front_cards = []
# generate front and back cards for each item in the playlist
for item in hipsterfy_playlist.get_tracks_data():
front_card = item.generate_hipsterfy_front_card(include_preview=False, card_style=CardStyle(card_style.value))
back_card = item.generate_hipsterfy_back_card(
primary_property=primary_item.value,
secondary_property=secondary_item.value,
additional_properties=additional_items.value,
card_style=CardStyle(card_style.value)
)
new_front_cards.append(pn.pane.HTML(front_card, sizing_mode='stretch_width'))
new_back_cards.append(pn.pane.HTML(back_card, sizing_mode='stretch_width'))
for card in new_front_cards:
front_cards_column.append(card)
for card in new_back_cards:
back_cards_column.append(card)
pn.state.notifications.success("Preview generated successfully!")
pn.state.notifications.info("Prepare to download the cards as HTML files...")
update_download_buttons()
if mode_selector.value == "2D":
# 2D-HTML wie gehabt
for item in hipsterfy_playlist.get_tracks_data():
front_card = item.generate_hipsterfy_front_card(include_preview=False, card_style=CardStyle(card_style.value))
back_card = item.generate_hipsterfy_back_card(
primary_property=primary_item.value,
secondary_property=secondary_item.value,
additional_properties=additional_items.value,
card_style=CardStyle(card_style.value)
)
new_front_cards.append(pn.pane.HTML(front_card, sizing_mode='stretch_width'))
new_back_cards.append(pn.pane.HTML(back_card, sizing_mode='stretch_width'))
for card in new_front_cards:
front_cards_column.append(card)
for card in new_back_cards:
back_cards_column.append(card)
update_download_buttons()
pn.state.notifications.success("2D preview generated successfully!")
else:
stl_buffers = []
for idx, item in enumerate(hipsterfy_playlist.get_tracks_data()):
stl_bytes = item.generate_3d_qr_card(
primary_property=primary_item.value,
secondary_property=secondary_item.value,
additional_properties=additional_items.value,
)
stl_filename = f"hipsterfy_card_{idx+1}.stl"
stl_buffers.append((stl_filename, stl_bytes))
front_cards_column.append(
pn.pane.Markdown(f"**3D Card {idx+1}:** `{stl_filename}` generated.", sizing_mode='stretch_width')
)
import zipfile
def zip_stls():
try:
buffer = BytesIO()
with zipfile.ZipFile(buffer, "w") as zf:
for fname, stl_bytes in stl_buffers:
zf.writestr(fname, stl_bytes)
buffer.seek(0)
return buffer
except Exception as e:
print("Error creating ZIP file:", e)
traceback.print_exc()
pn.state.notifications.error(f"Error creating ZIP file: {str(e)}", duration=10000)
return None
download_front_html_button.label = "Download 3D Cards (ZIP)"
download_front_html_button.callback = zip_stls
download_front_html_button.visible = True
download_back_html_button.visible = False
download_front_html_button.filename = "hipsterfy_3d_cards.zip"
pn.state.notifications.success("3D STL files generated! Download the ZIP from the sidebar.")
print_instructions.visible = mode_selector.value == "2d"
except Exception as e:
pn.state.notifications.error(f"Error generating preview: {str(e)}")
print("Error:", e)
traceback.print_exc()
pn.state.notifications.error(f"Error generating preview: {str(e)}", duration=10000)
finally:
front_cards_column.loading = False
back_cards_column.loading = False
@ -198,7 +250,16 @@ def create_panel_page(hipsterfy: Hipsterfy, playlist_uri: str=None, root_url = N
template = pn.template.FastListTemplate(
title='Hipsterfy',
sidebar=[
player_qr_pane, # <-- Dynamischer QR-Code ganz oben in der Sidebar
player_qr_pane,
pn.pane.HTML("<h3> Select card generation mode:</h3>", sizing_mode='stretch_width'),
pn.pane.HTML(
"""
<p>In 2D mode, you can print the cards directly from your browser. In 3D mode, STL files will be generated for 3d printing will be generated.</p>
""",
sizing_mode='stretch_width'
),
mode_selector,
pn.layout.Divider(),
playlist_instructions, playlist_uri, primary_item, secondary_item, additional_items, card_style,
cards_per_row_widget,
create_preview_button, download_front_html_button, download_back_html_button, print_instructions