Jupyter笔记本API获取笔记本电脑

时间:2017-09-07 07:25:30

标签: python jupyter-notebook jupyterhub

我的jupyter中心和单用户服务器正在运行。我正在使用Jupyter笔记本REST API来获取所有可用的笔记本。我正在使用http://127.0.0.1:8000/user/username/api/contents。有了这个,我能够在mu UI上看到所有可用的笔记本。

然而,当我尝试从我的python脚本执行相同操作时,我得到200个响应代码但无法提取任何内容,因为它重定向到登录页面,我需要为我的用户输入用户名和密码。我还尝试通过请求包传递这些凭据。像这样的东西

def bar_plot (data, n_slices, dx = 1, dy = 1, z_max = 1, x_label = 'x',
              y_label='y', z_label='z', elev_angle = 30, azim_angle = 115):
    """
    Makes a 3d bar plot of the data given as a 2d numpy array.

    Parameters
    ----------
    data: 2d-array
        Two-dimensional numpy array of z-values
    n_slices: int
        Number of 'slices' in y-directions to be used in the 3D plot
    dx: float
        Distance between neighboring x-positions
    dy: float
        Distance between neighboring y-positions
    x_label: str
        Label of the x-axis
    y_label: str
        Label of the y-axis
    z_lable: str
        Label of the z-axis
    elev_angle: int
        Alevation viewing angle
    azim_angle: int
        Azimuthal viewing angle
    z_max: float
        Default limit to the z-axis

    Returns
    -------
    fig: pyplot figure object
        Figure of the 3d-plot

    ax: pyplot axes object
        Axes object that contains the figure elements
    """

    # Initialize the figure object
    fig = plt.figure(figsize = [10, 8])
    ax = fig.add_subplot(111, projection='3d')

    # Colors to indicate variation in y-axis
    colors = sns.color_palette('viridis', n_colors=n_slices+1)

    # Dimensions of the 2d-array
    x_length, y_length = data.shape

    # Initial index of the slice
    i_slice = 0

    # Iterate through each slice and add bar plots
    for y in np.arange(0, y_length, y_length//n_slices):

        # x-, y- and z-positions
        x_pos = np.arange(x_length)*dx
        y_pos = y*np.ones(x_length)*dy
        z_pos = np.zeros(x_length)

        # Horizontal dimensions of the bars
        delta_x = dx*np.ones(x_length)
        delta_y = 2*dy*np.ones(x_length)

        # Heights in the z-direction
        delta_z = p[:,y]

        ax.bar3d(x_pos, y_pos, z_pos, delta_x, delta_y, delta_z, 
                 color = colors[i_slice])

        i_slice = i_slice + 1;

    # Add axis labels
    ax.set_xlabel(x_label)
    ax.set_ylabel(y_label)
    ax.set_zlabel(z_label)

    # Adjust the 3d viewing angle of the plot
    ax.view_init(elev_angle, azim_angle)

    # Set the z-limit of the plot
    z_max = np.min([z_max, np.max(data)])
    ax.set_zlim([0, z_max])

    return fig, ax

出局是:

import requests
api_url =  'http://127.0.0.1:8000/user/username/api/contents'
result = requests.get(api_url, auth=('username', 'password'))
print result
print result.json()

我对Jupyter很新。我错过了什么。在调用任何API之前,我还可以如何对用户进行身份验证。

在jupyerhub文档中看到这一点 - 请注意,API令牌授权JupyterHub REST API请求。相同的令牌不授权访问由JupyterHub管理的笔记本服务器提供的Jupyter Notebook REST API。使用不同的标记来访问Jupyter Notebook API。

所以这正是我正在寻找的。如何创建这个不同的令牌,以便访问jupyter笔记本API。

1 个答案:

答案 0 :(得分:2)

Jupyter笔记本需要一个访问令牌(这应该在你启动jupyter笔记本时显示)。在我的情况下,这样的事情,然后工作:

only_years = re.sub('\d+', replace_non_year_numbers, 'ab-c 0123 4r. a2017 2010')
no_special_char = re.sub('[^A-Za-z0-9 ]', ' ', only_years)
print(re.sub(' +', ' ', no_special_char))
# ab c r a2017 2010