Working on Flask version

This commit is contained in:
FrederikBaerentsen 2024-02-28 15:31:43 -05:00
parent 25a58eef54
commit 49de698ec9
4 changed files with 89 additions and 12 deletions

32
app.py
View File

@ -3,25 +3,45 @@ import json
app = Flask(__name__)
tmp = '71386-10'
@app.route('/')
def index():
return render_template('index.html')
with open('./sets/'+tmp+'/info.json') as info:
info_file = json.loads(info.read())
with open('./sets/'+tmp+'/inventory.json') as inventory:
inventory_file = json.loads(inventory.read())
return render_template('bootstrap_table.html', title=info_file['set_num']+" - "+info_file['name'],
info_file=info_file,inventory_file=inventory_file)
#return render_template('index.html')
#'Welcome to the Flask App'
@app.route('/saveNumber', methods=['POST'])
def save_number():
data = request.get_json()
data1 = request.form.get('brick.part.part_num')
data2 = request.form.get('brick.color.name')
number = request.form.get('numberInput')
if number is not None:
print(data1)
print(data2)
print(number)
with open('./info/'+tmp+'.json') as info:
json_file = json.loads(info.read())
print(json_file['count'])
print(json_file['unit'][0]['bricks']['missing'])
# Save number to JSON file
with open('data.json', 'w') as json_file:
json.dump({'number': number}, json_file)
return jsonify({'message': 'Number saved successfully'}), 200
else:
return jsonify({'error': 'Invalid data provided'}), 400
return ('', 204)
if __name__ == '__main__':
app.run(debug=True)
app.run(host='192.168.10.109', debug=True, port=3333)

12
lego.py
View File

@ -7,8 +7,6 @@ import rebrick #rebrickable api
# json things
import json
from bs4 import BeautifulSoup
import requests # request img from web
import shutil # save img locally
@ -34,9 +32,11 @@ with open('api','r') as f:
rb = rebrick.init(api_key)
Path(set_path).mkdir(parents=True, exist_ok=True)
Path('./static/parts').mkdir(parents=True, exist_ok=True)
Path('./info').mkdir(parents=True, exist_ok=True)
# Get set info
response = json.loads(rebrick.lego.get_set(int(set_num)).read())
response = json.loads(rebrick.lego.get_set(set_num).read())
with open(set_path+'info.json', 'w', encoding='utf-8') as f:
json.dump(response, f, ensure_ascii=False, indent=4)
@ -53,17 +53,17 @@ else:
logging.error('set_img_url: ' + set_num)
# set inventory
response = json.loads(rebrick.lego.get_set_elements(int(set_num)).read())
response = json.loads(rebrick.lego.get_set_elements(set_num).read())
with open(set_path+'inventory.json', 'w', encoding='utf-8') as f:
json.dump(response, f, ensure_ascii=False, indent=4)
# get part images if not exists
for i in response["results"]:
if not Path("./parts/"+i["element_id"]+".jpg").is_file():
if not Path("./static/parts/"+i["element_id"]+".jpg").is_file():
res = requests.get(i["part"]["part_img_url"], stream = True)
if res.status_code == 200:
with open("./parts/"+i["element_id"]+".jpg",'wb') as f:
with open("./static/parts/"+i["element_id"]+".jpg",'wb') as f:
shutil.copyfileobj(res.raw, f)
print('image saved')
else:

15
templates/base.html Normal file
View File

@ -0,0 +1,15 @@
<!doctype html>
<html>
<head>
<title>{{ title }}</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.1/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-+0n0xVW2eSR5OomGNYDnhzAbDsOXxcvSN1TPprVMTNDbiYZCxYbOOl7+AMvyTG2x" crossorigin="anonymous">
</head>
<body>
<div class="container">
<h1>{{ title }}</h1>
<hr>
{% block content %}{% endblock %}
</div>
{% block scripts %}{% endblock %}
</body>
</html>

View File

@ -0,0 +1,42 @@
{% extends "base.html" %}
{% block content %}
<table id="data" class="table table-striped">
<thead>
<tr>
<th>img</th>
<th>part.element_id</th>
<th>part.part_num</th>
<th>part.name</th>
<th>color.id</th>
<th>color.name</th>
<th>part.quantity</th>
<th>part.is_spare</th>
<th>Missing?</th>
</tr>
</thead>
<tbody>
{% for brick in inventory_file.results %}
<tr>
<td><img src="{{ '/static/parts/'+brick.element_id+'.jpg' }}" style="height: 50px; width: auto;"></td>
<td>{{ brick.element_id }}</td>
<td>{{ brick.part.part_num }}</td>
<td>{{ brick.part.name }}</td>
<td>{{ brick.color.id }}</td>
<td>{{ brick.color.name }}</td>
<td>{{ brick.quantity }}</td>
<td>{{ brick.is_spare }}</td>
<td>
<form action="/saveNumber" method="post">
<input type="hidden" name="brick.part.part_num" value="{{ brick.part.part_num }}">
<input type="hidden" name="brick.color.name" value="{{ brick.color.name }}">
<label for="numberInput">Number:</label>
<input type="text" id="numberInput" name="numberInput">
<button type="submit">Save Number</button>
</form>
</td>
</tr>
{% endfor %}
</tbody>
</table>
{% endblock %}