Skip to content

Instantly share code, notes, and snippets.

@CharlyWargnier
Last active January 21, 2023 21:12
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save CharlyWargnier/d3bdf62a301263e8874905a63f2d999d to your computer and use it in GitHub Desktop.
Save CharlyWargnier/d3bdf62a301263e8874905a63f2d999d to your computer and use it in GitHub Desktop.
from zipfile import ZipFile
from io import BytesIO
zipObj = ZipFile("sample.zip", "w")
# Add multiple files to the zip
zipObj.write("checkpoint")
zipObj.write("model_hyperparameters.json")
# close the Zip File
zipObj.close()
ZipfileDotZip = "sample.zip"
with open(ZipfileDotZip, "rb") as f:
bytes = f.read()
b64 = base64.b64encode(bytes).decode()
href = f"<a href=\"data:file/zip;base64,{b64}\" download='{ZipfileDotZip}.zip'>\
Click last model weights\
</a>"
st.sidebar.markdown(href, unsafe_allow_html=True)
@joefodor
Copy link

joefodor commented May 9, 2022

thanks for this! FYI you forgot to add import base64 :)

@SMDGLogisticsSRl
Copy link

hi, do you know, why the zipped file can not be opened..?

@ThomasEvooq
Copy link

Thank you for this! In my program, the user filtrate a data framework using streamlit input, and then downloads one pdf per resulting data frame line. So, I'm trying to create a zip with "on the fly" content, depending on the current filtering. Could you help with that?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment