Hacktoberfest 2026: những issue maintainer đã đánh dấu cho tháng Mười, đang mở và phù hợp người mới. Xem issue Hacktoberfest

Better cell magic by removing some of the syntax limitations

Đang mở
#1,196 0 bình luận 0 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

Đánh giá

Độ khó
5/5
Thời gian dự kiến
Hơn một tuần
Mức phù hợp với người mới
25/100
Loại issue
Tính năng
Độ rõ ràng
Cần làm rõ
Mức độ hoạt động
Đình trệ
Công nghệ
jupyter-notebook, python

Hướng nghiên cứu

Issue đề xuất năm thay đổi liên quan đến cú pháp cell-magic, bao gồm vị trí đặt biểu thức, kết thúc khối, đối số, fallback của line-magic và tiếp diễn. Không có tệp triển khai, điểm vào của parser hoặc bài kiểm thử nào được nêu tên; trước tiên hãy xác định mã phân tích cú pháp và dịch cell-magic, sau đó thiết lập các bài kiểm thử tương thích riêng cho từng đề xuất và xác định cú pháp nào được chấp nhận.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

Below, I'm making suggestions (A)-(E) how I think cell magic could be made even better.

If people generally think these are good ideas, I'd try and make a PR.

First, however, I wanted to see if there are any syntax or compatibility issues I'm missing.

As far as I can tell, each of my suggestions
(1) extends the set of legal cell contents
and all but (B) and (E)
(2) gives the same results on the previous set of legal cell contents
where the only reason that (B) and (E) do not give the same results on previous contents is that nested magic would trip (which I actually use quite frequently)

If I required triple-% for the new functionality, I think all my suggestions would satisfy both (1) and (2).

(A) Just about any cell magic I've ever used would have been better if it was possible to assign output via

df = %%sql
<SOME QUERY>

similar to how it's already possible to do

df = %sql <SOME QUERY>.

More generally, I think it should simply be allowed to start %%-magic anywhere in a cell.

Most cell magic I know instead implement awkward workarounds like

%%sql -o df
<SOME QUERY>

but since every cell magic uses its own syntax and most don't support things like assigning to dictionaries, this is a major hassle.

(B) Extending on the previous item, it'd be great if we could also have code after %%-magic, e.g.,

df = %%sql
<SOME QUERY>
%%

i.e., be able to close a cell magic block by %% (see intro: maybe best to just use triple-%)

(C) Just about any cell magic I've ever used would have been better if it was possible to do

%%sql(connection=a, flag=b)
<SOME QUERY>

and have this be translated to a regular function-call of the function registered as part of the cell-magic registration, the result of which would then do the actual cell-magic.

Most cell magic I know instead implements awkward workarounds like sql -f -conn conn but since every cell magic uses its own syntax and most don't support things like indexing dictionaries, this is a major hassle.

For example, the sql magic above could be implemented by registering

def sql_cell_magic_wrapper(connection: Conn = None, flag: bool = None) -> Func[str, Any]:
    def cell_magic(cell: str) -> Any:
        return sql_client.execute(cell)

with Jupyter; and a simple %%sql without arguments could simply be translated to a call of sql_cell_magic_wrapper without arguments, i.e., sql_cell_magic_wrapper()(<cell>)

(D) Cell magic should be allowed to fall back to line magic:

%%sql <SOME QUERY>

It's annoying to always have to remove/add % just because you're changing your mind whether you need an entire cell or not.

(E) Further to (D), it should be legal to terminate a single-line %%-magic and continue the line non-magically, e.g.

df = (%%sql <SOME QUERY> %%).sum()

(A-E): In combination, it would be possible to do

import pandas as pd
import numpy as np
import my_magic

df = pd.DataFrame({'i': np.arange(13), 'v': np.random.rand(13)})
agg = %%sql(progress_bar=False)
SELECT 
i, v 
FROM df 
WHERE i % 2 == 0
%%
agg.plot(x='i', y='v')

or, if one so prefers,

import pandas as pd
import numpy as np
import my_magic

df = pd.DataFrame({'i': np.arange(13), 'v': np.random.rand(13)})
%%sql(progress_bar=False) SELECT i, v FROM df WHERE i % 2 == 0 %%.plot(x='i', y='v')
Ngôn ngữ chính
Python
Star
734
Fork
411
Merge trung bình
1 ngày 2 giờ
Pull request đã merge (30 ngày)
9

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của ipython/ipykernel

Tất cả issue của ipython/ipykernel

Issue tương tự

Thêm issue về Python

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.