Git Product home page Git Product logo

Comments (5)

galaunay avatar galaunay commented on August 16, 2024 4

Nice !
Thanks for giving us feedback.

If having to write the entire virtualenv path annoys you, this block :

#+BEGIN_SRC elisp :results silent :session poetry_virtual_env
(pyvenv-activate "~/.cache/pypoetry/virtualenvs/my-poetry-virtual-env-xUtoYcEX-py3.7/")
#+END_SRC

could be replaced with this simpler one:

#+BEGIN_SRC elisp :results silent :session poetry_virtual_env
(poetry-venv-workon)
#+END_SRC

It will select the adequate virtualenv automatically.

Maybe even simpler, you could use property lines and just add this line at the top of your org file:

# -*- eval: (poetry-venv-workon); -*-

This will automatically activate the poetry virtualenv when opening your org file.

from poetry.el.

galaunay avatar galaunay commented on August 16, 2024

I gave a quick try to your snippet and it works fine for me.

The only thing is that I had to restart the babel session (the one you named 'default').
If the session is started outside of the poetry virtualenv, it stays that way until you restart it.
Maybe the problem you are experiencing could come from that ?

from poetry.el.

Ziip-dev avatar Ziip-dev commented on August 16, 2024

I have a very similar question :)
My plan was:

  • create a poetry project in a version controlled directory
  • literate program in an org file (within my org roam directory)
  • run source blocks within the poetry virtual env to test my code
  • tangle everything to .py files in the project directory

Is this feasible? I have not figured it out yet (specifically the third point)

from poetry.el.

Ziip-dev avatar Ziip-dev commented on August 16, 2024

I have figured this out, in case some people are interested in the process here is a link to the reddit question for more details.
In summary:

* create a poetry project in a version controlled directory

This is the easy part, I just used basic poetry and git commands (or poetry.el and magit at your convenience).

* literate program in an org file

With babel functionalities, again, pretty straight forward with the documentation.

* run source blocks within the poetry virtual env to test my code

The trickiest part: a session must first be spawned in the corresponding virtual env with:

#+BEGIN_SRC elisp :results silent :session poetry_virtual_env
(pyvenv-activate "~/.cache/pypoetry/virtualenvs/my-poetry-virtual-env-xUtoYcEX-py3.7/")
#+END_SRC

Then following python blocks will execute in this virtual env session:

#+BEGIN_SRC python :results output :session poetry_virtual_env
import sys
print(sys.executable)
#+END_SRC

#+RESULTS:
: ~/.cache/pypoetry/virtualenvs/my-poetry-virtual-env-xUtoYcEX-py3.7/bin/python3
* tangle everything to .py files in the project directory

Tangle deletes the targeted file by default for each new tangled block. I found this excellent stackexchange answer which appends different blocks using noweb-ref before tangling. Very elegant :)
Here is a stripped down example:

#+BEGIN_SRC shell :noweb yes :exports none :mkdirp yes :tangle /tmp/accumulated.sh
<<accumulated>>
#+END_SRC

#+BEGIN_SRC shell :noweb-ref accumulated
echo "Hello"
#+END_SRC

#+BEGIN_SRC shell :noweb-ref accumulated
echo "World"
#+END_SRC

Now I can dev in org files and use literate programming functionalities while being able to test the code in the foreign virtual environment with the right dependencies before tangling everything in its corresponding .py file in the project directory.
Isn't that neat? 😁

from poetry.el.

Ziip-dev avatar Ziip-dev commented on August 16, 2024

Very interesting, thank you for the tips!
How does it work if my org file is in a different location than the poetry project?

I also noticed that I don't need to define a session for the code block activating the virtual env: simply running (pyvenv-activate) in my org file activates the right virtual env and all python blocks execute in this env afterward. Could you please provide some insights on the question? :)

from poetry.el.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.