-
Notifications
You must be signed in to change notification settings - Fork 214
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'UTSAVS26:main' into main
- Loading branch information
Showing
5 changed files
with
132 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
## Barcode Generator | ||
### 🎯 Goal | ||
The goal of this project is to create a barcode generator using Streamlit. The application allows users to generate EAN-13, EAN-8, and UPCA barcodes based on a user-provided numeric code, and provides the ability to download the generated barcode image. | ||
|
||
### 🧾 Description | ||
This project is a barcode generator built with Streamlit, providing an easy-to-use interface for generating barcodes. Users can select from multiple barcode formats (EAN-13, EAN-8, UPCA), input the appropriate numeric code, and generate a barcode image. The generated barcode is displayed and can be downloaded as a PNG file. | ||
|
||
### 🧮 What I had done! | ||
Developed a Streamlit interface for selecting barcode types and entering barcode numbers. | ||
Validated the input based on the selected barcode format. | ||
Generated and displayed barcodes using the python-barcode library. | ||
Added functionality for users to download the generated barcode image. | ||
### 📚 Libraries Needed | ||
Streamlit - For building the user interface. | ||
python-barcode - To generate the barcodes. | ||
base64 - For encoding the barcode image to display and download in the Streamlit app. | ||
### 📢 Conclusion | ||
The barcode generator offers a simple and effective way for users to generate and download barcodes in various formats. The app handles input validation and provides a clean interface for creating barcodes, making it a useful tool for generating barcodes on demand. | ||
|
||
|
||
**Akarsh Ghildyal** | ||
[GitHub](https://github.com/AkarshGhildyal) | [LinkedIn](https://www.linkedin.com/in/akarsh-ghildyal/) | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
from barcode.writer import ImageWriter | ||
from barcode import EAN13, EAN8, UPCA | ||
import streamlit as st | ||
import base64 | ||
import os | ||
|
||
# Set barcode type | ||
BARCODE_TYPE = {"EAN-13": [13, EAN13], "EAN-8": [8, EAN8], "UPCA": [12, UPCA]} | ||
|
||
def barCodeGenerator(): | ||
st.markdown( | ||
""" | ||
<h1 style='text-align: center; color: #FF4B4B; font-family: Verdana;'> | ||
BAR CODE GENERATOR📊 | ||
</h1> | ||
""", | ||
unsafe_allow_html=True, | ||
) | ||
|
||
box = st.container() # To keep everything inside one container | ||
with box: | ||
option = st.radio( | ||
"Select type of Barcode", ["EAN-13", "EAN-8", "UPCA"], horizontal=True | ||
) | ||
num = st.text_input( | ||
"Enter barcode number", | ||
value="", | ||
max_chars=BARCODE_TYPE[option][0], | ||
placeholder=f"Enter {BARCODE_TYPE[option][0]} digits long barcode number", | ||
) | ||
button_div = st.empty() # So that when Generate Barcode is pressed, it will be replaced by Reset button | ||
|
||
with button_div: | ||
if st.button("Generate barcode"): | ||
generate(num, box, option) | ||
st.button("Reset barcode") # Resets everything | ||
|
||
|
||
def generate(num, box, option): | ||
with box: | ||
if len(num) != BARCODE_TYPE[option][0] or not num.isnumeric(): | ||
st.warning( | ||
f"Please enter a valid {option} barcode of {BARCODE_TYPE[option][0]} digits!!" | ||
) | ||
else: | ||
# Path for the image | ||
image_path = "assets/barcode" | ||
|
||
# Create the 'assets' directory if it doesn't exist | ||
if not os.path.exists('assets'): | ||
os.makedirs('assets') | ||
|
||
# Generate the barcode and save it | ||
my_code = BARCODE_TYPE[option][1](num, writer=ImageWriter()) | ||
my_code.save(image_path) | ||
|
||
# Open the saved image and encode it for display | ||
with open(f"{image_path}.png", "rb") as file: | ||
image_data = file.read() | ||
|
||
encoded_image = base64.b64encode(image_data).decode() | ||
|
||
# Display the barcode image and download button | ||
st.markdown( | ||
f""" | ||
<style> | ||
.button-container {{ | ||
display: flex; | ||
justify-content: space-around; | ||
}} | ||
.styled-button {{ | ||
background-color: #ff4b4b; | ||
color: white; | ||
border: none; | ||
padding: 10px 20px; | ||
border-radius: 5px; | ||
cursor: pointer; | ||
transition: background-color 0.3s, color 0.3s; | ||
}} | ||
.styled-button:hover {{ | ||
background-color: #ff3333; | ||
color: white; | ||
}} | ||
</style> | ||
<div style="display: flex; flex-direction: column; align-items: center;"> | ||
<img src="data:image/png;base64,{encoded_image}" alt="Your Image"> | ||
<br> | ||
<a href="data:image/png;base64,{encoded_image}" download="barcode.png"> | ||
<button class="styled-button">Download Image</button> | ||
</a> | ||
</div> | ||
""", | ||
unsafe_allow_html=True, | ||
) | ||
|
||
barCodeGenerator() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
streamlit==1.26.0 | ||
python-barcode==0.14.0 | ||
Pillow==10.0.1 | ||
base64==1.1.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters