Skip to content

Commit

Permalink
Fix for config dict with strings vs lists, closes #18
Browse files Browse the repository at this point in the history
Also includes datasette fix from #16
  • Loading branch information
simonw committed Nov 20, 2023
1 parent 33d0e4b commit 211c2c6
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 10 deletions.
4 changes: 2 additions & 2 deletions datasette_enrichments/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ async def enrich_data_post(datasette, request, enrichment, stuff):
Form = await enrichment.get_config_form(db, table)

form = Form(post_vars)

if not form.validate():
# TODO: Fix this
return Response.html(
Expand All @@ -169,8 +170,7 @@ async def enrich_data_post(datasette, request, enrichment, stuff):
)
)

config = post_vars._data.copy()
config.pop("csrftoken", None)
config = {field.name: field.data for field in form}

# Initialize any necessary tables
await enrichment.initialize(datasette, db, table, config)
Expand Down
6 changes: 3 additions & 3 deletions example-enrichments/jinja_sandbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class ConfigForm(Form):

async def initialize(self, datasette, db, table, config):
# Ensure column exists
output_column = config["output_column"][0]
output_column = config["output_column"]

def add_column_if_not_exists(conn):
db = sqlite_utils.Database(conn)
Expand All @@ -58,8 +58,8 @@ async def enrich_batch(
job_id: int,
):
env = SandboxedEnvironment(enable_async=True)
template = env.from_string(config["template"][0])
output_column = config["output_column"][0]
template = env.from_string(config["template"])
output_column = config["output_column"]
for row in rows:
output = await template.render_async({"row": row})
await db.execute_write(
Expand Down
5 changes: 3 additions & 2 deletions example-enrichments/openai_embeddings.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,16 +87,17 @@ async def initialize(self, datasette, db, table, config):

async def enrich_batch(
self,
datasette,
db: Database,
table: str,
rows: List[dict],
pks: List[str],
config: dict,
job_id: int,
):
template = SpaceTemplate(config["template"][0])
template = SpaceTemplate(config["template"])
texts = [template.safe_substitute(row) for row in rows]
token = config["api_token"][0]
token = config["api_token"]

async with httpx.AsyncClient() as client:
response = await client.post(
Expand Down
5 changes: 3 additions & 2 deletions example-enrichments/uppercase.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
from datasette_enrichments import Enrichment
from datasette.database import Database
from typing import List
from wtforms import Form, SelectField
from wtforms import Form, SelectMultipleField
from wtforms.widgets import ListWidget, CheckboxInput
import asyncio


class MultiCheckboxField(SelectField):
class MultiCheckboxField(SelectMultipleField):
widget = ListWidget(prefix_label=False)
option_widget = CheckboxInput()

Expand All @@ -27,6 +27,7 @@ class ConfigForm(Form):

async def enrich_batch(
self,
datasette,
db: Database,
table: str,
rows: List[dict],
Expand Down
1 change: 1 addition & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ class ConfigForm(Form):

async def enrich_batch(
self,
datasette,
db: Database,
table: str,
rows: List[dict],
Expand Down
2 changes: 1 addition & 1 deletion tests/test_enrichments.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,4 @@ async def test_uppercase_plugin(tmpdir, is_root):
# It should be queued up
assert db.execute(
"select status, enrichment, database_name, table_name, config from _enrichment_jobs"
).fetchall() == [("p", "uppercasedemo", "data", "t", '{"columns": ["s"]}')]
).fetchall() == [("p", "uppercasedemo", "data", "t", '{"columns": "s"}')]

0 comments on commit 211c2c6

Please sign in to comment.