Skip to content

Commit 4d7eeea

Browse files
committed
Launch REPL with python -m juliacall
1 parent 951e093 commit 4d7eeea

File tree

2 files changed

+96
-0
lines changed

2 files changed

+96
-0
lines changed

pysrc/juliacall/__main__.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
from juliacall import Main
2+
3+
RED = "\033[1;31m"
4+
GREEN = "\033[1;32m"
5+
RESET = "\033[0m"
6+
7+
import os
8+
path_to_banner = os.path.join(os.path.dirname(__file__), "banner.jl")
9+
Main.seval(f"include(\"{path_to_banner}\"); banner()")
10+
11+
while True:
12+
try:
13+
line = input(f"{GREEN}juliacall> {RESET}")
14+
except EOFError:
15+
break
16+
if not line.strip():
17+
continue
18+
try:
19+
result = Main.seval(line)
20+
if result is not None:
21+
print(result)
22+
except Exception as e:
23+
print(f"{RED}ERROR:{RESET} {e}")
24+

pysrc/juliacall/banner.jl

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
# https://github.com/JuliaLang/julia/blob/fae0d0ad3e5d9804533435fe81f4eaac819895af/stdlib/REPL/src/REPL.jl#L1727C1-L1795C4
2+
3+
function banner(io::IO = stdout; short = false)
4+
if Base.GIT_VERSION_INFO.tagged_commit
5+
commit_string = Base.TAGGED_RELEASE_BANNER
6+
elseif isempty(Base.GIT_VERSION_INFO.commit)
7+
commit_string = ""
8+
else
9+
days = Int(floor((ccall(:jl_clock_now, Float64, ()) - Base.GIT_VERSION_INFO.fork_master_timestamp) / (60 * 60 * 24)))
10+
days = max(0, days)
11+
unit = days == 1 ? "day" : "days"
12+
distance = Base.GIT_VERSION_INFO.fork_master_distance
13+
commit = Base.GIT_VERSION_INFO.commit_short
14+
15+
if distance == 0
16+
commit_string = "Commit $(commit) ($(days) $(unit) old master)"
17+
else
18+
branch = Base.GIT_VERSION_INFO.branch
19+
commit_string = "$(branch)/$(commit) (fork: $(distance) commits, $(days) $(unit))"
20+
end
21+
end
22+
23+
commit_date = isempty(Base.GIT_VERSION_INFO.date_string) ? "" : " ($(split(Base.GIT_VERSION_INFO.date_string)[1]))"
24+
25+
if get(io, :color, false)::Bool
26+
c = Base.text_colors
27+
tx = c[:normal] # text
28+
jl = c[:normal] # julia
29+
jc = c[:blue] # juliacall
30+
d1 = c[:bold] * c[:blue] # first dot
31+
d2 = c[:bold] * c[:red] # second dot
32+
d3 = c[:bold] * c[:green] # third dot
33+
d4 = c[:bold] * c[:magenta] # fourth dot
34+
35+
if short
36+
print(io,"""
37+
$(d3)o$(tx) | Version $(VERSION)PythonCall: $(PythonCall.VERSION)
38+
$(d2)o$(tx) $(d4)o$(tx) | $(commit_string)
39+
""")
40+
else
41+
print(io,""" $(d3)_$(tx)
42+
$(d1)_$(tx) $(jl)_$(tx) $(d2)_$(d3)(_)$(d4)_$(tx)$(jc) _ _ $(tx) | Documentation: https://juliapy.github.io/PythonCall.jl/
43+
$(d1)(_)$(jl) | $(d2)(_)$(tx) $(d4)(_)$(tx)$(jc) | || |$(tx) |
44+
$(jl)_ _ _| |_ __ _$(jc) ___ __ _ | || |$(tx) | Julia: $(VERSION)
45+
$(jl)| | | | | | |/ _` |$(jc)/ __|/ _` || || |$(tx) | PythonCall: $(PythonCall.VERSION)
46+
$(jl)| | |_| | | | (_| |$(jc) |__ (_| || || |$(tx) |
47+
$(jl)_/ |\\__'_|_|_|\\__'_|$(jc)\\___|\\__'_||_||_|$(tx) | This REPL is running via Python using JuliaCall.
48+
$(jl)|__/$(tx) | Only basic features are supported.
49+
50+
""")
51+
end
52+
else
53+
if short
54+
print(io,"""
55+
o | Version $(VERSION)PythonCall: $(PythonCall.VERSION)
56+
o o | $(commit_string)
57+
""")
58+
else
59+
print(io,"""
60+
_
61+
_ _ _(_)_ _ _ | Documentation: https://juliapy.github.io/PythonCall.jl/
62+
(_) | (_) (_) | || | |
63+
_ _ _| |_ __ _ ___ __ _ | || | | Julia: $(VERSION)
64+
| | | | | | |/ _` |/ __|/ _` || || | | PythonCall: $(PythonCall.VERSION)
65+
| | |_| | | | (_| | |__ (_| || || | |
66+
_/ |\\__'_|_|_|\\__'_|\\___|\\__'_||_||_| | This REPL is running via Python using JuliaCall.
67+
|__/ | Only basic features are supported.
68+
69+
""")
70+
end
71+
end
72+
end

0 commit comments

Comments
 (0)