Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,6 @@ cython_debug/
# frontend build assets in preswald server
preswald/static/assets/
preswald/static/index.html
temp**
preswald_project/

notes.md
Expand Down
39 changes: 32 additions & 7 deletions preswald/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,35 +24,52 @@ def cli():
pass


def _create_default_init_files(target_dir: str, project_slug: str):
def get_available_templates():
templates_dir = os.path.join(os.path.dirname(__file__), "templates")
if not os.path.exists(templates_dir):
return []
return [
name
for name in os.listdir(templates_dir)
if os.path.isdir(os.path.join(templates_dir, name))
]


def _create_default_init_files(target_dir: str, project_slug: str, template: str):
"""Create default project files in the target directory using templates."""
from importlib.resources import as_file, files

# Read and write hello.py from template
with as_file(files("preswald").joinpath("templates/hello.py.template")) as path:
with as_file(
files("preswald").joinpath(f"templates/{template}/hello.py.template")
) as path:
with open(path) as f:
hello_content = f.read()
with open(os.path.join(target_dir, "hello.py"), "w") as f:
f.write(hello_content)

# Read and write preswald.toml from template
with as_file(
files("preswald").joinpath("templates/preswald.toml.template")
files("preswald").joinpath(f"templates/{template}/preswald.toml.template")
) as path:
with open(path) as f:
toml_content = f.read().format(project_slug=project_slug)
with open(os.path.join(target_dir, "preswald.toml"), "w") as f:
f.write(toml_content)

# Read and write secrets.toml from template
with as_file(files("preswald").joinpath("templates/secrets.toml.template")) as path:
with as_file(
files("preswald").joinpath(f"templates/{template}/secrets.toml.template")
) as path:
with open(path) as f:
secrets_content = f.read()
with open(os.path.join(target_dir, "secrets.toml"), "w") as f:
f.write(secrets_content)

# Read and write sample.csv from template
with as_file(files("preswald").joinpath("templates/sample.csv.template")) as path:
with as_file(
files("preswald").joinpath(f"templates/{template}/data/sample.csv.template")
) as path:
with open(path) as f:
sample_data = f.read()
with open(os.path.join(target_dir, "data", "sample.csv"), "w") as f:
Expand All @@ -61,7 +78,13 @@ def _create_default_init_files(target_dir: str, project_slug: str):

@cli.command()
@click.argument("name", default="preswald_project")
def init(name):
@click.option(
"--template",
type=click.Choice(get_available_templates(), case_sensitive=True),
default="default",
help="Use a template to initialize the project.",
)
def init(name, template):
"""
Initialize a new Preswald project.
Creates a directory with basic project structure.
Expand All @@ -87,19 +110,21 @@ def init(name):
shutil.copy2(path, os.path.join(name, "images", "logo.png"))

# Create basic project files
_create_default_init_files(name, project_slug)
_create_default_init_files(name, project_slug, template)

# Track initialization
telemetry.track_command(
"init",
{
"project_name": name,
"project_slug": project_slug,
"template": template,
},
)

click.echo(f"Initialized a new Preswald project in '{name}/' 🎉!")
click.echo(f"Project slug: {project_slug}")
click.echo(f"Template used: {template}")
except Exception as e:
click.echo(f"Error initializing project: {e} ❌")

Expand Down
8,566 changes: 8,566 additions & 0 deletions preswald/templates/comparison/data/sample.csv.template

Large diffs are not rendered by default.

45 changes: 45 additions & 0 deletions preswald/templates/comparison/hello.py.template
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
from preswald import connect, get_df, text, query,table, slider, plotly, sidebar, Workflow
import plotly.express as px

connect()
workflow = Workflow()
df = get_df('sample_csv')

#i want a slider that maintains the minimum rate and then two charts that tell about number of movies for en and hi per year that exceeds that number of rate

text("# Analysis of movies on IMDb")
text("## Language vs Rating")

min_rate = slider(
label="Minimum Vote Average",
min_val=5,
max_val=10,
step=0.1,
default=7
)

en_query = f"""
SELECT COUNT(*) AS num, substr(release_date,1,4) AS release_year
FROM sample_csv
WHERE original_language='en' AND CAST(vote_average AS float)>{min_rate}
GROUP BY release_year
ORDER BY release_year
"""

hi_query = f"""
SELECT COUNT(*) AS num, substr(release_date,1,4) AS release_year
FROM sample_csv
WHERE original_language='hi' AND CAST(vote_average AS float)>{min_rate}
GROUP BY release_year
ORDER BY release_year
"""


en_data=query(en_query, 'sample_csv')
hi_data=query(hi_query, 'sample_csv')

en_fig = px.bar(en_data,x='release_year',y='num',title=f'Movies in English higher than {min_rate}')
hi_fig = px.bar(hi_data,x='release_year',y='num',title=f'Movies in Hindi higher than {min_rate}')

plotly(en_fig,size=0.5)
plotly(hi_fig,size=0.5)
Loading