-
Notifications
You must be signed in to change notification settings - Fork 78
[RFC] Module scripts #649
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
Closed
Closed
[RFC] Module scripts #649
Changes from 22 commits
Commits
Show all changes
27 commits
Select commit
Hold shift + click to select a range
4d7eeea
Launch REPL with `python -m juliacall`
klamike 21108f3
Init Julia with `python -m juliacall.init`
klamike c9cdad8
Colorize errors
klamike 0fb6059
Handle `KeyboardInterrupt` in REPL
klamike cbe0537
More closely match Julia's REPL
klamike be3897b
Use `Base.run_main_repl`
klamike eb357e6
Update copy
klamike 64be410
Revert error color change
klamike c8df2e7
Refactor
klamike a60c530
Fix banner logic
klamike ceae6c7
Fix short banner colors
klamike f79928d
Refactor config to argparse
klamike 698e16e
Refactor
klamike 121b36e
Add name guard to init
klamike 1fa8b98
`pyjulia` -> `juliapy`
klamike bd2c6cb
Remove script
klamike 6acaeba
Add `-e`/`--eval` and refactor argparse
klamike 5070ea1
Add `-E`/`--print`
klamike 972f47f
Add message for `-e` and `-E` assert
klamike 846d823
Allow kwargs for `juliacall.repl` api
klamike 6631df6
Clean up imports
klamike 8939efa
Make `main` function in `__main__`
klamike 64bf6b5
Update pysrc/juliacall/repl.py
klamike 33a9c67
Add test
klamike 66eedae
Don't check PythonCall.jl version
klamike c83c309
Simplify test
klamike 6e60d4c
Fix string in fstring for 3.9
klamike File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| import argparse | ||
|
|
||
| from juliacall import Main | ||
| from juliacall.repl import run_repl, add_repl_args | ||
|
|
||
|
|
||
| def main(): | ||
| parser = argparse.ArgumentParser("JuliaCall REPL (experimental)") | ||
| parser.add_argument('-e', '--eval', type=str, default=None, help='Evaluate <expr>. If specified, all other arguments are ignored.') | ||
| parser.add_argument('-E', '--print', type=str, default=None, help='Evaluate <expr> and display the result. If specified, all other arguments are ignored.') | ||
|
|
||
| add_repl_args(parser) | ||
|
|
||
| args = parser.parse_args() | ||
| assert not (args.eval is not None and args.print is not None), "Cannot specify both -e/--eval and -E/--print" | ||
|
|
||
| if args.eval is not None: | ||
| Main.seval(args.eval) | ||
| elif args.print is not None: | ||
| result = Main.seval(args.print) | ||
| Main.display(result) | ||
| else: | ||
| run_repl( | ||
| banner=args.banner, | ||
| quiet=args.quiet, | ||
| history_file=args.history_file, | ||
| preamble=args.preamble | ||
| ) | ||
|
|
||
| if __name__ == '__main__': | ||
| main() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| # https://github.com/JuliaLang/julia/blob/fae0d0ad3e5d9804533435fe81f4eaac819895af/stdlib/REPL/src/REPL.jl#L1727C1-L1795C4 | ||
|
|
||
| function __PythonCall_banner(banner_opt::Symbol = :yes) | ||
| io = stdout | ||
|
|
||
| if banner_opt == :no | ||
| return | ||
| end | ||
|
|
||
| short = banner_opt == :short | ||
|
|
||
| if get(io, :color, false)::Bool | ||
| c = Base.text_colors | ||
| tx = c[:normal] # text | ||
| jl = c[:normal] # julia | ||
| jc = c[:blue] # juliacall text | ||
| jb = c[:bold] * jc # bold blue dot | ||
| d1 = c[:bold] * c[:blue] # first dot | ||
| d2 = c[:bold] * c[:red] # second dot | ||
| d3 = c[:bold] * c[:green] # third dot | ||
| d4 = c[:bold] * c[:magenta] # fourth dot | ||
| d5 = c[:bold] * c[:yellow] # bold yellow dot | ||
|
|
||
| if short | ||
| print(io,""" | ||
| $(jb)o$(tx) | Julia $(VERSION) | ||
| $(jb)o$(tx) $(d5)o$(tx) | PythonCall $(PythonCall.VERSION) | ||
| """) | ||
| else | ||
| print(io,""" $(d3)_$(tx) | ||
| $(d1)_$(tx) $(jl)_$(tx) $(d2)_$(d3)(_)$(d4)_$(tx)$(jc) _ _ $(tx) | Documentation: https://juliapy.github.io/PythonCall.jl/ | ||
| $(d1)(_)$(jl) | $(d2)(_)$(tx) $(d4)(_)$(tx)$(jc) | || |$(tx) | | ||
| $(jl)_ _ _| |_ __ _$(jc) ___ __ _ | || |$(tx) | Julia: $(VERSION) | ||
| $(jl)| | | | | | |/ _` |$(jc)/ __|/ _` || || |$(tx) | PythonCall: $(PythonCall.VERSION) | ||
| $(jl)| | |_| | | | (_| |$(jc) |__ (_| || || |$(tx) | | ||
| $(jl)_/ |\\__'_|_|_|\\__'_|$(jc)\\___|\\__'_||_||_|$(tx) | The JuliaCall REPL is experimental. | ||
| $(jl)|__/$(tx) | | ||
|
|
||
| """) | ||
| end | ||
| else | ||
| if short | ||
| print(io,""" | ||
| o | Julia $(VERSION) | ||
| o o | PythonCall $(PythonCall.VERSION) | ||
| """) | ||
| else | ||
| print(io,""" | ||
| _ | ||
| _ _ _(_)_ _ _ | Documentation: https://juliapy.github.io/PythonCall.jl/ | ||
| (_) | (_) (_) | || | | | ||
| _ _ _| |_ __ _ ___ __ _ | || | | Julia: $(VERSION) | ||
| | | | | | | |/ _` |/ __|/ _` || || | | PythonCall: $(PythonCall.VERSION) | ||
| | | |_| | | | (_| | |__ (_| || || | | | ||
| _/ |\\__'_|_|_|\\__'_|\\___|\\__'_||_||_| | The JuliaCall REPL is experimental. | ||
| |__/ | | ||
| """) | ||
| end | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| import json | ||
| import juliapkg | ||
| import sys | ||
|
|
||
| if __name__ == '__main__': | ||
| # invoking python -m juliacall.init automatically imports juliacall which | ||
| # calls init() which calls juliapkg.executable() which lazily downloads julia | ||
|
|
||
| if "--debug" in sys.argv: | ||
| state = juliapkg.state.STATE | ||
| state["version"] = str(state["version"]) | ||
| print(json.dumps(state, indent=2)) | ||
| else: | ||
| print("Initialized successfully. Pass --debug to see the full JuliaPkg state.") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| import argparse | ||
| import os | ||
| from pathlib import Path | ||
|
|
||
| from juliacall import Main, Base | ||
|
|
||
| def run_repl(banner='yes', quiet=False, history_file='yes', preamble=None): | ||
| os.environ.setdefault("PYTHON_JULIACALL_HANDLE_SIGNALS", "yes") | ||
| if os.environ.get("PYTHON_JULIACALL_HANDLE_SIGNALS") != "yes": | ||
| print("Experimental JuliaCall REPL requires PYTHON_JULIACALL_HANDLE_SIGNALS=yes") | ||
| exit(1) | ||
|
|
||
| Base.is_interactive = True | ||
|
|
||
| if not quiet: | ||
| Main.include(os.path.join(os.path.dirname(__file__), 'banner.jl')) | ||
| Main.__PythonCall_banner(Base.Symbol(banner)) | ||
|
|
||
| if Main.seval(r'VERSION > v"v1.11.0-alpha1"'): | ||
| no_banner_opt = Base.Symbol("no") | ||
| else: | ||
| no_banner_opt = False | ||
|
|
||
| if preamble: | ||
| Main.include(str(preamble.resolve())) | ||
|
|
||
| Base.run_main_repl( | ||
| Base.is_interactive, | ||
| quiet, | ||
| no_banner_opt, | ||
| history_file == 'yes', | ||
| True | ||
| ) | ||
|
|
||
| def add_repl_args(parser): | ||
| parser.add_argument('--banner', choices=['yes', 'no', 'short'], default='yes', help='Enable or disable startup banner') | ||
| parser.add_argument('--quiet', '-q', action='store_true', help='Quiet startup: no banner, suppress REPL warnings') | ||
| parser.add_argument('--history-file', choices=['yes', 'no'], default='yes', help='Load or save history') | ||
| parser.add_argument('--preamble', type=Path, help='Code to be included before the REPL starts') | ||
|
|
||
| if __name__ == '__main__': | ||
| parser = argparse.ArgumentParser("JuliaCall REPL (experimental)") | ||
| add_repl_args(parser) | ||
| args = parser.parse_args() | ||
| run_repl(args.banner, args.quiet, args.history_file, args.preamble) | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.