Skip to content

Commit

Permalink
Added button positioning logic
Browse files Browse the repository at this point in the history
  • Loading branch information
ThodorisTsampouris authored Oct 17, 2024
2 parents 2b675c6 + fac797d commit f62a592
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 11 deletions.
40 changes: 34 additions & 6 deletions codintxt/api/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,6 @@ async def validate_b64(base64_model: str, api_key: str = Security(get_api_key)):
raise HTTPException(status_code=400, detail=f"Validation error: {e}")



@api.post("/generate/json")
async def gen_json(
gen_auto_model: TransformationModel = Body(...),
Expand Down Expand Up @@ -156,6 +155,7 @@ async def gen_json(
)
return resp


@api.post("/generate/streamlit")
async def gen_streamlit_code(
gen_auto_model: TransformationModel = Body(...),
Expand Down Expand Up @@ -188,12 +188,39 @@ async def gen_streamlit_code(
)
steps.append({"range": [step_min, step_max], "color": color})
component["steps"] = steps
max_rows = max(component["position"]["r"] for component in json_model["components"])
max_columns = max(component["position"]["c"] for component in json_model["components"])
if component["ctype"] == "ButtonGroup":
if component["alignBtns"] == "Horizontal":
for index in range(len(component["buttons"])):
button = component["buttons"][index]
if "position" not in button:
button["position"] = {}
button["position"]["c"] = component["position"]["c"] + index
button["position"]["r"] = component["position"]["r"]
if component["alignBtns"] == "Vertical":
for index in range(len(component["buttons"])):
button = component["buttons"][index]
if "position" not in button:
button["position"] = {}
button["position"]["r"] = component["position"]["r"] + index
button["position"]["c"] = component["position"]["c"]
max_rows = 1
max_columns = 1
for component in json_model["components"]:
position = component["position"]
r = position["r"]
c = position["c"]
max_rows = max(max_rows, r)
max_columns = max(max_columns, c)
if "buttons" in component:
for button in component["buttons"]:
button_position = button["position"]
button_r = button_position["r"]
button_c = button_position["c"]
max_rows = max(max_rows, button_r)
max_columns = max(max_columns, button_c)

rendered_code = templates.get_template("streamlit.py.jinja").render(
model=json_model,
max_rows = max_rows,
max_columns = max_columns
model=json_model, max_rows=max_rows, max_columns=max_columns
)
resp["message"] = "Codintxt-2-Streamlit Transformation success"
resp["code"] = rendered_code
Expand All @@ -206,6 +233,7 @@ async def gen_streamlit_code(
)
return resp


@api.post("/generate/json/file")
async def gen_json_file(
model_file: UploadFile = File(...), api_key: str = Security(get_api_key)
Expand Down
11 changes: 6 additions & 5 deletions codintxt/m2t/streamlit.py.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ class {{button.label}}Node(Node):
)
def publish(self):
self.pub.publish({{button.payload | safe}})
print(f"Published to topic: {self.topic}")

{% endfor %}
{% endif %}
Expand Down Expand Up @@ -110,14 +109,16 @@ def main():
{% if component.ctype == "Gauge" and component.position.r - 1 == row%}

{# Initializes the placeholder of each widget #}
{{component.name}}_placeholder = columns{{row}}[{{component.position.c}} - 1].empty()
{{component.name}}_placeholder = columns{{row}}[{{component.position.c - 1}}].empty()
{%endif%}
{% if component.ctype == "ButtonGroup" and component.position.r - 1 == row%}
{% if component.ctype == "ButtonGroup"%}
{# Creates buttons #}
{% for button in component.buttons %}
with columns{{row}}[{{component.position.c - 1 + loop.index0}}]:
{% if button.position.r - 1 == row %}
with columns{{row}}[{{button.position.c - 1}}]:
if st.button("{{button.label}}"):
{{button.label}}_node.publish()
print(f"Button pressed")
{% endif %}
{% endfor %}
{%endif%}
{%endfor%}
Expand Down

0 comments on commit f62a592

Please sign in to comment.