
Write Python inline in your Rust code - Aissen
https://docs.rs/inline-python/
======
ccostes
I'm in the process of porting some DSP code from python to rust and was really
wishing I could use matplotlib to check my intermediate data. If this works
like it looks like it should it would totally blow my mind!

~~~
m-ou-se
Matplotlib was one of the reasons I made this. Works just fine:
[https://github.com/dronesforwork/inline-
python/blob/aebabc88...](https://github.com/dronesforwork/inline-
python/blob/aebabc886e863bcf49e3d93c05e44907ea5ad9a4/inline-python-
example/src/main.rs)

~~~
ccostes
I liked rust already, but this is amazing!

------
_bxg1
Neat example of the power of macros, but actually using it in a project you
want to maintain seems incredibly code-smelly.

------
m-ou-se
Author here. Let me know if you have any questions. :)

~~~
dbcurtis
How does this interact with Rust memory safety? I am not familiar with the
PyToObject trait, so I am guessing that this has some kind of safety wrapper?

~~~
m-ou-se
In Python, all data (even simple integers) are all allocated on the heap.
ToPyObject makes a deep copy of everything, converting everything to
`PyObject` on the heap, so all data is owned.

The numpy crate helps if you need to convert large arrays to Python, as numpy
does not store every element separately on the heap.

~~~
maccard
> In Python, all data (even simple integers) are all allocated on the heap.

Total sidebar, this leads to one of my favourite programming language "quirks"
\- you can redefine the value of 1! [http://hforsten.com/redefining-the-
number-2-in-python.html](http://hforsten.com/redefining-the-number-2-in-
python.html)

------
oblio
Cool hack.

Though I'd definitely advise wanting to do this to Do The Right Thing™ and
just put each thing in its own file.

~~~
rcthompson
I imagine the common usage would be to write an ordinary python module and
then just have a small block of inline python in the Rust code that imports
and calls the module.

------
crimsonalucard
I remember a while back I saw something where you can do javascript inline
with your python. You know with javascript you can do jsx and inline html with
your javascript.

This is the ultimate combo making for the ultimate language. Inline html,
inside javascript inside python inside rust. This is the future of
programming.

~~~
nicoburns
There are also crates to use JavaScript inline in Rust. Rust also has fairly
complete bindings to the DOM APIs that can be used when targetting web
assembly :)

~~~
crimsonalucard
Wow. Next step is to inline the entire JVM and all languages related to it
like clojure and scala into rust. Then take rust and inline it into golang
then take golang and inline it back into rust.

I can't wait for the future.

------
airstrike
Cool, but can anyone explain to me why I'd want this?

~~~
andolanra
The author mentioned on Twitter that it was useful for quickly plotting data
in Rust, with this example:

    
    
        fn main() {
            let data = vec![(4, 3), (2, 8), (3, 1), (4, 0)];
            python! {
                import matplotlib.pyplot as plt
                plt.plot('data)
                plt.show()
            }
        }
    

Source:
[https://twitter.com/m_ou_se/status/1118255841961025539](https://twitter.com/m_ou_se/status/1118255841961025539)

~~~
m0zg
This snippet right here might actually convince me to learn Rust. I work in
C++ a lot, and deal with problems where visualization would be useful if it
were easy. But because nothing in C++ is ever easy, I end up not doing it in a
lot of cases, sometimes to my own detriment.

------
pariahHN
This could give a nice way to build desktop apps with rust - make your GUI in
Python but the guts are rust. Possibly more straightforward than building
libraries in rust and generating python bindings.

------
pascoej
I've been looking into doing this in Go and haven't found a good solution for
Python 3.

Is it possible to build a static binary (including Python) with pyo3 in rust?

~~~
bjourne
Doesn't Go have a C FFI? You just call libpython. I'd imagine adding syntactic
sugar is harder because Go doesn't have the same metaprogramming capabilities,
otherwise it should work the same.

~~~
pascoej
I’m not worried about the interop. I want a not too hacky way tool to
statically link it in.

