Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to run cell containing Python script (within Pluto) #258

Closed
danvinci opened this issue Jan 17, 2023 · 3 comments
Closed

How to run cell containing Python script (within Pluto) #258

danvinci opened this issue Jan 17, 2023 · 3 comments

Comments

@danvinci
Copy link

danvinci commented Jan 17, 2023

Hi!
Thank you for this package, switching over from PyCall.

I'm using Pluto and I had a cell that had only Python code:

begin
py"""

from package import something

def function pyfun():
    return something()

"""
julia_funWrap = py"pyfun"

This would allow me to define several helper functions (that call other functions) all in Python, and then wrap those functions so that I can call them from Julia when needed.

Is it possible to do something similar in PythonCall?

I have a script that I'd like to reuse, the alternative being writing Julia helper functions instead, wrapping the inner calls, as well as prepare the necessary inputs so that they can be passed.

This example seems to point instead towards writing "hybrid" Python/Julia code:
https://discourse.julialang.org/t/passing-parameters-to-a-python-function-via-pycall-that-need-to-be-in-a-dict-form/84691/4

@danvinci danvinci changed the title How to run python script within Pluto How to run cell containing Python script within Pluto Jan 17, 2023
@danvinci danvinci changed the title How to run cell containing Python script within Pluto How to run cell containing Python script (within Pluto) Jan 17, 2023
@danvinci
Copy link
Author

danvinci commented Jan 17, 2023

Update
I'm having more fun (and appreciating) rewriting the script as Julia functions, making the necessary mappings and type conversions before/after calling each wrapped (Python) function.

Also: realized only later Discussions were enabled, this shouldn't be an issue.

It would still be interesting to know if is possible to run entire Python scripts residing in a cell (not an external script.py), the documentation wasn't explicit about this.

@cjdoris
Copy link
Collaborator

cjdoris commented Jan 20, 2023

It's possibly a bit buried, but there are functions pyexec and pyeval and macros @pyexec and @pyeval for evaluating or executing Python code.

https://cjdoris.github.io/PythonCall.jl/stable/pythoncall-reference/#PythonCall.@pyexec

Your example using PyCall can be rewritten using PythonCall as

@pyexec """
from package import something

global pyfun
def function pyfun():
    return something()
"""
julia_funWrap = @pyeval "pyfun"

You need global pyfun because @pyexec introduces a new local scope.

@danvinci
Copy link
Author

Thank you!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants