File size: 3,579 Bytes
38c4ff0
 
a038650
 
 
 
 
38c4ff0
 
 
a038650
38c4ff0
a038650
 
 
38c4ff0
a038650
38c4ff0
 
 
a038650
 
 
 
38c4ff0
 
a038650
38c4ff0
 
 
a038650
 
 
38c4ff0
a038650
 
38c4ff0
 
 
 
a038650
 
 
 
 
38c4ff0
a038650
 
38c4ff0
 
a038650
 
 
 
 
 
38c4ff0
a038650
38c4ff0
 
a038650
 
 
 
 
 
38c4ff0
 
 
 
 
a038650
 
 
38c4ff0
a038650
 
38c4ff0
 
b301009
 
 
 
 
 
 
 
38c4ff0
 
a038650
b301009
a038650
 
38c4ff0
 
 
a038650
 
 
 
 
 
 
38c4ff0
a038650
38c4ff0
 
a038650
 
 
 
 
 
 
38c4ff0
a038650
38c4ff0
 
a038650
 
 
 
 
 
bb7f3a2
a038650
 
38c4ff0
a038650
38c4ff0
 
a038650
 
 
 
38c4ff0
a038650
38c4ff0
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
import marimo

__generated_with = "0.19.9"
app = marimo.App(
    width="medium",
    app_title="Context Template Drafting Marimo Notebook",
)


@app.cell
def _():
    import marimo as mo
    from wigglystuff import SortableList
    from jsonpath import JSONPath
    import json

    return JSONPath, json, mo


@app.cell
def _():
    from FUNCTIONS.json_template_drafting_functions import (
        json_to_marimo_ui,
        create_download_button,
    )

    return create_download_button, json_to_marimo_ui


@app.cell(hide_code=True)
def _(mo):
    mo.md(r"""
    ### **Intro**

    This marimo notebook allows you to upload a **.json** file *(with or without values)* and get a fillable version of the template structure that you can fill out and download as a completed template. ***Upload a file by clicking on the area below or draging and dropping a file onto it.***
    """)
    return


@app.cell
def _(mo):
    file_upload = mo.ui.file(
        filetypes=[".json"],
        label="**Upload JSON Template** (*Click and Select or Drag & Drop*)",
        kind="area",
    )
    file_upload
    return (file_upload,)


@app.cell
def _(file_upload, json):
    example_preview = (
        json.loads(file_upload.contents())
        if file_upload.value is not None and file_upload.contents() is not None
        else None
    )
    return (example_preview,)


@app.cell
def _(example_preview, file_upload, mo):
    (
        mo.accordion({"Preview Example (after loading file)": example_preview})
        if file_upload.value is not None and file_upload.contents() is not None
        else mo.md("*Upload a JSON File to Preview the Template*")
    )
    return


@app.cell(hide_code=True)
def _(mo):
    mo.md(r"""
    ### **Edit the values**

    Within the template below you can edit the values of the different inputs and when you define a filename and press **Download** it will save the **.json** file locally on your computer. ***Above you can also see a collapsable preview with filled out values if your template had any***
    """)
    return

@app.cell
def _(mo):
    replicate_values = mo.ui.checkbox(
        label="**Prefill Values From Template** *(if present)*"
    )
    replicate_values
    return (replicate_values,)


@app.cell
def _(file_upload, json_to_marimo_ui):
    json_form = json_to_marimo_ui(file_upload, replicate_values=replicate_values.value)
    json_form
    return (json_form,)


@app.cell
def _(JSONPath, json_form):
    client_name_in_template = (
        (JSONPath("$..client_name").parse(json_form.value) or [""])[0]
        .lower()
        .replace(" ", "_")
        if json_form.value is not None
        else ""
    )
    return (client_name_in_template,)


@app.cell
def _(client_name_in_template, mo):
    save_file_name = mo.ui.text(
        label="**Download File Name:**",
        value=client_name_in_template if client_name_in_template else "",
        full_width=True,
        placeholder="add_file_name",
    )
    return (save_file_name,)


@app.cell
def _(create_download_button, json_form, save_file_name):
    download_output = create_download_button(
        json_form.value,
        filename_prefix=(
            save_file_name.value
            if save_file_name.value is not None
            else "downloaded_template_output"
        ),
    )
    return (download_output,)


@app.cell
def _(download_output, mo, save_file_name):
    download_stack = mo.vstack(
        [save_file_name, download_output], justify="space-around", gap=1
    )
    download_stack
    return


if __name__ == "__main__":
    app.run()