{"id":17439351,"url":"https://github.com/m-ou-se/inline-python","last_synced_at":"2025-05-14T20:07:28.167Z","repository":{"id":34646660,"uuid":"181870598","full_name":"m-ou-se/inline-python","owner":"m-ou-se","description":"Inline Python code directly in your Rust code","archived":false,"fork":false,"pushed_at":"2025-03-15T19:30:25.000Z","size":113,"stargazers_count":1191,"open_issues_count":15,"forks_count":43,"subscribers_count":16,"default_branch":"master","last_synced_at":"2025-04-14T18:03:44.983Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"https://docs.rs/inline-python","language":"Rust","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"bsd-2-clause","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/m-ou-se.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2019-04-17T10:40:19.000Z","updated_at":"2025-04-14T03:52:18.000Z","dependencies_parsed_at":"2024-01-14T10:59:31.953Z","dependency_job_id":"8b52b23a-7519-4bf5-8ef5-afdd67f97c4c","html_url":"https://github.com/m-ou-se/inline-python","commit_stats":{"total_commits":101,"total_committers":7,"mean_commits":"14.428571428571429","dds":0.3366336633663366,"last_synced_commit":"dce5fcdcaab9c6abdd3f04728470423a9a38f303"},"previous_names":["m-ou-se/inline-python"],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/m-ou-se%2Finline-python","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/m-ou-se%2Finline-python/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/m-ou-se%2Finline-python/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/m-ou-se%2Finline-python/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/m-ou-se","download_url":"https://codeload.github.com/m-ou-se/inline-python/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248933339,"owners_count":21185460,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-10-17T13:02:41.434Z","updated_at":"2025-05-14T20:07:28.155Z","avatar_url":"https://github.com/m-ou-se.png","language":"Rust","readme":"# inline-python\n\nInline Python code directly in your Rust code.\n\n## Example\n\n```rust\nuse inline_python::python;\n\nfn main() {\n    let who = \"world\";\n    let n = 5;\n    python! {\n        for i in range('n):\n            print(i, \"Hello\", 'who)\n        print(\"Goodbye\")\n    }\n}\n```\n\n## How to use\n\nUse the `python!{..}` macro to write Python code directly in your Rust code.\n\n_NOTE:_ Rust **nightly** toolchain is required. Feature `proc_macro_span` is still unstable, for more details check out [issue #54725 - \nTracking issue for `proc_macro::Span` inspection APIs](https://github.com/rust-lang/rust/issues/54725)\n\n### Using Rust variables\n\nTo reference Rust variables, use `'var`, as shown in the example above.\n`var` needs to implement `pyo3::ToPyObject`.\n\n### Re-using a Python context\n\nIt is possible to create a `Context` object ahead of time and use it for running the Python code.\nThe context can be re-used for multiple invocations to share global variables across macro calls.\n\n```rust\nlet c = Context::new();\n\nc.run(python! {\n  foo = 5\n});\n\nc.run(python! {\n  assert foo == 5\n});\n```\n\nAs a shortcut, you can assign a `python!{}` invocation directly to a\nvariable of type `Context` to create a new context and run the Python code\nin it.\n\n```rust\nlet c: Context = python! {\n  foo = 5\n};\n\nc.run(python! {\n  assert foo == 5\n});\n```\n\n### Getting information back\n\nA `Context` object could also be used to pass information back to Rust,\nas you can retrieve the global Python variables from the context through\n`Context::get`.\n\n```rust\nlet c: Context = python! {\n  foo = 5\n};\n\nassert_eq!(c.get::\u003ci32\u003e(\"foo\"), 5);\n```\n\n### Syntax issues\n\nSince the Rust tokenizer will tokenize the Python code, some valid Python\ncode is rejected. The main things to remember are:\n\n- Use double quoted strings (`\"\"`) instead of single quoted strings (`''`).\n\n  (Single quoted strings only work if they contain a single character, since\n  in Rust, `'a'` is a character literal.)\n\n- Use `//`-comments instead of `#`-comments.\n\n  (If you use `#` comments, the Rust tokenizer will try to tokenize your\n  comment, and complain if your comment doesn't tokenize properly.)\n\n- Write `f \"\"` instead of `f\"\"`.\n\n  (String literals with prefixes, like `f\"\"`, are reserved in Rust for\n  future use. You can write `f \"\"` instead, which is automatically\n  converted back to to `f\"\"`.)\n\nOther minor things that don't work are:\n\n- The `//` and `//=` operators are unusable, as they start a comment.\n\n  Workaround: you can write `##` instead, which is automatically converted\n  to `//`.\n\n- Certain escape codes in string literals.\n  (Specifically: `\\a`, `\\b`, `\\f`, `\\v`, `\\N{..}`, `\\123` (octal escape\n  codes), `\\u`, and `\\U`.)\n\n  These, however, are accepted just fine: `\\\\`, `\\n`, `\\t`, `\\r`, `\\xAB`\n  (hex escape codes), and `\\0`.\n\n- Raw string literals with escaped double quotes. (E.g. `r\"...\\\"...\"`.)\n\n- Triple-quoted byte- and raw-strings with content that would not be valid\n  as a regular string. And the same for raw-byte and raw-format strings.\n  (E.g. `b\"\"\"\\xFF\"\"\"`, `r\"\"\"\\z\"\"\"`, `fr\"\\z\"`, `br\"\\xFF\"`.)\n\nEverything else should work fine.\n","funding_links":[],"categories":["Rust","Build Tools \u0026 Frameworks"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fm-ou-se%2Finline-python","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fm-ou-se%2Finline-python","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fm-ou-se%2Finline-python/lists"}