3

I'm trying to get Taipy core working with Sqlite but don't succeed. Taipy doesn't seem to be able to connect.

Does anyone have a small working example?

Regards, Markus

I've tried to connect with e.g. the following settings:

  • No username and password
  • db_name: taipy and also taipy.db
  • db_engine: sqlite
  • with import sqlite3 and without

sales_history_cfg = Config.configure_sql_data_node(
    id="sales_history",
    db_username="",
    db_password="",
    db_name="taipy",
    db_engine="sqlite",
    read_query="SELECT * from sales",
    write_query_builder=write_query_builder
)

# Configuration of tasks
task_cfg = Config.configure_task("double",
    double,
    sales_history_cfg,
    output_data_node_cfg)

# Configuration of the pipeline and scenario
pipeline_cfg = Config.configure_pipeline("my_pipeline", [task_cfg])
scenario_cfg = Config.configure_scenario("my_scenario", [pipeline_cfg])

# Run of the Core
tp.Core().run()
Markus
  • 33
  • 4
  • Hey Markus, Feel free to join our Discord Server discord.com/invite/SNNsCDj9? and connect with like-minded individuals and collaborate on your web application buildings :) – Rym Guerbi Michaut Jul 17 '23 at 09:53

2 Answers2

3

Have you previously created a SQL database with this username, password, and name? Taipy facilitates the connection to an already-existent SQL database but will not create one for you. You will then be able to use it like any other Data Nodes with all the features of Taipy Core.

There are many different ways to create a Database using a Cloud provider or other tools. You can find some information here.

Florian Jacta
  • 1,143
  • 1
  • 2
  • 5
  • The database exists and SQLite doesn't support username and password. The error that's returned is: self._engine = create_engine(self._conn_string()) File "/Users/administrator/python_projects/virt1/lib/python3.10/site-packages/taipy/core/data/abstract_sql.py", line 165, in _conn_string extra_args = {**extra_args, "driver": driver} TypeError: 'NoneType' object is not a mapping – Markus Mar 06 '23 at 16:53
3

I assume you are using taipy version 2.1.3. Which is the latest as of today.

The error you are facing is that db_extra_args should be provided in the configuration of your data node. An empty dictionary should work. db_extra_args={}.

I noticed 3 other (potential) issues in your code:

  1. A missing semicolon ";" at the end of the SQL query.
  2. A missing path parameter. It should point to the folder containing the SQLite database file. If the path is not provided, Taipy will search for it in the root folder. (In my example I have my database stored in the file "../data/sqlite/taipy.db")
  3. The db_name should include the file extension. (In my example it is "taipy.db")

Below is the code I used to make your code work in my environment. I previously created a sales table (CREATE TABLE sales (date int, nb_sales int);) in the database "../data/sqlite/taipy.db".

from taipy import Config
import taipy as tp
import pandas as pd


def double(data):
    return "WHATEVER"


def write_query_builder(data: pd.DataFrame):
    insert_data = list(
        data[["date", "nb_sales"]].itertuples(index=False, name=None))
    return [
        "DELETE FROM sales;",
        ("INSERT INTO sales VALUES (?, ?);", insert_data)
    ]


sales_cfg = Config.configure_sql_data_node(
    id="sales_history",
    db_username="",
    db_password="",
    db_extra_args={},
    db_name="taipy.db",
    db_engine="sqlite",
    read_query="SELECT * from sales;",
    write_query_builder=write_query_builder,
    path="../data/sqlite/"
)

output_data_node_cfg = Config.configure_data_node(id="output")

# Configuration of tasks
task_cfg = Config.configure_task("double", double, sales_cfg, output_cfg)

# Configuration of the pipeline and scenario
pipeline_cfg = Config.configure_pipeline("my_pipeline", [task_cfg])
scenario_cfg = Config.configure_scenario("my_scenario", [pipeline_cfg])

# Run of the Core
tp.Core().run()

Note that when no db_extra_args is needed, it will not be necessary to provide an empty dictionary anymore in the next version 2.2 (The target date is May 2023 for the release). A documentation enhancement is also planned for 2.2, particularly more examples for all supported db_engines (SQLite, mssql, MySQL, or PostgreSQL).

  • Thanks for the example, I got it working. But taipy requires the file dbname on disk to be: 'taipy.db.sqlite3' and 'taipy.db' in the config. If the db can't be found taipy will create it with '.sqlite3' added to the name. – Markus Mar 10 '23 at 10:57
  • are you sure you are working with taipy 2.1.3 and not the version 2.1.2? 'cause there is a fix for that in 2.1.3. – Jean-Robin Medori Mar 12 '23 at 07:26