|
1 |
| -## CodeQuery使用教程 |
| 1 | +## CodeFuse-Query使用教程 |
2 | 2 |
|
3 |
| -### 使用Github Codespaces 来体验CodeQuery分析能力 |
| 3 | +### 使用Github Codespaces 来体验CodeFuse-Query分析能力 |
| 4 | + |
| 5 | +### Jupyter kernel for CodeFuse-query 介绍 |
| 6 | + |
| 7 | +Jupyter kernel for CodeFuse-query 是 Jupyter 的一个特定内核,它为 Jupyter 环境提供了与 CodeFuse-query 相关的交互式功能。该内核允许用户在 Jupyter Notebook 中编写和执行 Godel 脚本,用于抽取、查询代码数据。此外,它还支持编写 Python 代码,以便对查询到的数据进行更深入的数据可视化和数据后处理操作。这里我们使用这个内核编写 Jupyter Notebook,用来进行 CodeFuse-Query 教程的学习和实践。 |
4 | 8 |
|
5 | 9 | #### 步骤
|
6 | 10 |
|
7 |
| -- 在项目主页切换到目标分支 |
8 | 11 | - 创建 Codespaces,依次点击 Code -> Codespaces,在当前分支创建一个 Codespaces
|
9 | 12 | - 创建后,打开该 Codespaces,加载完成后,切换至项目 tutorial/notebook 目录下
|
10 |
| -- 选择示例下的 jupyter notebook 分析教程,即可开始体验 |
| 13 | +- 选择示例下的 Jupyter Notebook 分析教程,即可开始体验 |
11 | 14 |
|
12 | 15 | #### 注意
|
13 |
| -- 打开jupyter页面之后,如果是第一次加载容器,你还需要配置教程所使用的内核,在右上角 “选择内核”弹出框中,依次选择 “Juypter Kernel...” -> “Godel Kernel” 即可。 |
| 16 | + |
| 17 | +在打开 Jupyter 页面之后,如果是第一次加载容器,你还需要配置教程所使用的 `Jupyter kernel for CodeFuse-query`。在右上角的 "选择内核" 弹出框中,依次选择 "Jupyter Kernel..." -> "Godel Kernel"。 |
| 18 | + |
| 19 | +#### Kernel 用法 |
| 20 | +##### 设置要查询的 COREF database |
| 21 | + |
| 22 | +使用 `%db /path/to/db` 魔法命令来设置COREF db路径,内核会读取这个值来进行query查询,比如: |
| 23 | + |
| 24 | +```bash |
| 25 | +%db ./db |
| 26 | +``` |
| 27 | + |
| 28 | +> tips: 还可以用`!`来运行一些有用的bash命令,比如查看Sparrow CLI是否存在: |
| 29 | +
|
| 30 | +```rust |
| 31 | +!which sparrow |
| 32 | +``` |
| 33 | + |
| 34 | +##### Godel 查询 |
| 35 | + |
| 36 | +设置好db路径后可以直接写Godel脚本,Jupyter kernel会创建临时文件来保存当前运行的Godel脚本并调用`sparrow query run`命令来进行查询,结果返回以HTML格式显示: |
| 37 | + |
| 38 | + |
| 39 | +```rust |
| 40 | +// Write your query and run in the notebook |
| 41 | +// ... |
| 42 | +``` |
| 43 | + |
| 44 | + |
| 45 | +`%db` 魔法命令可以写在 Godel 脚本的第一行: |
| 46 | + |
| 47 | + |
| 48 | +```rust |
| 49 | +%db ./db |
| 50 | +// Write your query and run in the notebook |
| 51 | +// ... |
| 52 | +``` |
| 53 | + |
| 54 | + |
| 55 | +可以通过 `%%save_to` cell魔法命令保存**上一次运行成功**的 query 结果到一个JSON文件,具体用法: |
| 56 | +```bash |
| 57 | +%%save_to PATH - save the query result to a JSON file. |
| 58 | + |
| 59 | +This cell magic will save the Sparrow query result to a file. |
| 60 | + |
| 61 | +Example: |
| 62 | + %%save_to /path/to/file.json |
| 63 | +``` |
| 64 | + |
| 65 | +比如: |
| 66 | +```bash |
| 67 | +%%save_to ./query.json |
| 68 | +``` |
| 69 | + |
| 70 | +##### 运行 Python 代码 |
| 71 | + |
| 72 | +可以通过 `%%python` 或者 `%python` 来运行 Python 代码,比如: |
| 73 | + |
| 74 | +```python |
| 75 | +%%python |
| 76 | +import pandas as pd |
| 77 | +data = pd.read_json('./query.json') |
| 78 | +data.sort_values('cmplx', ascending=False, inplace=True) |
| 79 | +top_10 = data.head(10) |
| 80 | +print(top_10) |
| 81 | +``` |
| 82 | + |
| 83 | +```python |
| 84 | +%python print('hello') |
| 85 | +``` |
0 commit comments