https://github.com/ekzhang/inline-sql Skip to content Toggle navigation Sign up * Product + Actions Automate any workflow + Packages Host and manage packages + Security Find and fix vulnerabilities + Codespaces Instant dev environments + Copilot Write better code with AI + Code review Manage code changes + Issues Plan and track work + Discussions Collaborate outside of code + Explore + All features + Documentation + GitHub Skills + Blog * Solutions + By Plan + Enterprise + Teams + Compare all + By Solution + CI/CD & Automation + DevOps + DevSecOps + Case Studies + Customer Stories + Resources * Open Source + GitHub Sponsors Fund open source developers + The ReadME Project GitHub community articles + Repositories + Topics + Trending + Collections * Pricing [ ] * # In this repository All GitHub | Jump to | * No suggested jump to results * # In this repository All GitHub | Jump to | * # In this user All GitHub | Jump to | * # In this repository All GitHub | Jump to | Sign in Sign up {{ message }} ekzhang / inline-sql Public * Notifications * Fork 0 * Star 26 Inline SQL in any Python program License MIT license 26 stars 0 forks Star Notifications * Code * Issues 0 * Pull requests 0 * Actions * Projects 0 * Security * Insights More * Code * Issues * Pull requests * Actions * Projects * Security * Insights ekzhang/inline-sql This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository. main Switch branches/tags [ ] Branches Tags Could not load branches Nothing to show {{ refName }} default View all branches Could not load tags Nothing to show {{ refName }} default View all tags 1 branch 1 tag Code * Clone HTTPS GitHub CLI [https://github.com/e] Use Git or checkout with SVN using the web URL. [gh repo clone ekzhan] Work fast with our official CLI. Learn more. * Open with GitHub Desktop * Download ZIP Launching GitHub Desktop If nothing happens, download GitHub Desktop and try again. Launching GitHub Desktop If nothing happens, download GitHub Desktop and try again. Launching Xcode If nothing happens, download Xcode and try again. Launching Visual Studio Code Your codespace will open once ready. There was a problem preparing your codespace, please try again. Latest commit @ekzhang ekzhang Update the README again ... 041aa8e Oct 7, 2022 Update the README again 041aa8e Git stats * 16 commits Files Permalink Failed to load latest commit information. Type Name Latest commit message Commit time .github/workflows Run tests on Python 3.8 and 3.9 Oct 8, 2022 inline_sql Bump version to 0.1.0 Oct 8, 2022 tests Add tests on a couple real CSV files Oct 8, 2022 .editorconfig Add tests on a couple real CSV files Oct 8, 2022 .gitignore Initial commit Oct 6, 2022 LICENSE Initial commit Oct 6, 2022 README.md Update the README again Oct 8, 2022 pyproject.toml Try to test on Python 3.7 Oct 8, 2022 View code Inline SQL Installation Usage Library Use Acknowledgements README.md Inline SQL PyPI - Version PyPI - Python Version A simple embedded language for running inline SQL in Python programs. from inline_sql import sql, sql_val assert sql_val^ "SELECT 1 + 1" == 2 x = 5 assert sql_val^ "SELECT $x * 2" == 10 df = sql^ "SELECT * FROM (VALUES (1, 10), (2, 20)) df (x, y)" assert sql_val^ "SELECT SUM(x) + SUM(y) FROM df" == 33 Operations in the inline_sql library run directly inside your process. You can query local datasets (pandas frames), CSV files, and even interpolate variables seamlessly. This is implemented as a small wrapper around DuckDB, so it is extremely fast. Installation Supports Python 3.7+, tested on all major operating systems. pip install inline-sql Usage The exported sql and sql_val variables are magic objects that can be used to run queries. Queries can read from local dataframes by name, and they can embed parameters using dollar-sign notation. >>> from inline_sql import sql, sql_val >>> sql_val^ "SELECT 1 + 1" 2 >>> x = 5 >>> sql_val^ "SELECT 2 * $x" 10 >>> sql^ "SELECT * FROM 'disasters.csv' LIMIT 5" Entity Year Deaths 0 All natural disasters 1900 1267360 1 All natural disasters 1901 200018 2 All natural disasters 1902 46037 3 All natural disasters 1903 6506 4 All natural disasters 1905 22758 >>> def total_deaths(entity: str) -> float: ... return sql_val^ "SELECT SUM(deaths) FROM disasters WHERE Entity = $entity" ... >>> total_deaths("Drought") 11731294.0 >>> total_deaths("Earthquake") 2576801.0 You can run any SQL query as described in the DuckDB documentation. Library Use You can use inline_sql as a library. Since results from queries are ordinary pandas.DataFrame objects, they work in functions and application code. Here's a longer example: import pandas as pd from inline_sql import sql, sql_val def head_data(count: int) -> pd.DataFrame: return sql^ "SELECT * FROM 'cars.csv' LIMIT $count" cars = head_data(50) origin_counts = sql^ """ SELECT origin, COUNT() FROM cars GROUP BY origin ORDER BY count DESC """ print(origin_counts) most_common = origin_counts.origin[0] print(sql_val^ """ SELECT AVG(horsepower) FROM cars WHERE origin = $most_common """) In general, sql_val is used to run scalar queries, while sql is used to run queries that return tables. Acknowledgements Created by Eric Zhang (@ekzhang1). Licensed under the MIT license. About Inline SQL in any Python program Topics python sql dsl olap query-language duckdb Resources Readme License MIT license Stars 26 stars Watchers 1 watching Forks 0 forks Releases 1 0.1.0 Latest Oct 8, 2022 Languages * Python 100.0% Footer (c) 2022 GitHub, Inc. Footer navigation * Terms * Privacy * Security * Status * Docs * Contact GitHub * Pricing * API * Training * Blog * About You can't perform that action at this time. You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.