将代码附加到 jupyter 笔记本中现有文件的神奇命令

问题描述 投票:0回答:1

我正在创建 jupyter 笔记本教程来运行 Streamlit 应用程序。 我使用

%%writefile dashboard_ui.py
将单元代码写入给定文件。

%%writefile app.py
import streamlit as st

st.title('Hello Streamlit')
st.write('This is a simple Streamlit app running in Jupyter Notebook!')

使用相同的魔法命令只需覆盖文件内的代码即可。

使用什么神奇命令将代码插入文件而不覆盖旧代码?

我尝试将代码片段保存在不同的文件中。虽然这是替代解决方案,但必须有更简单的方法来附加到单个文件。

python jupyter-notebook ipython magic-command
1个回答
0
投票

我想通了!标记

-a

%%writefile app.py
import streamlit as st

st.title('Hello Streamlit')
st.write('This is a simple Streamlit app running in Jupyter Notebook!')

下次可以用

%%writefile -a app.py  # flag -a -> indicate the insertion

with st.sidebar:
    st.title('South Korea Economic Dashboard')
    year_list = list(df.columns)[::-1]

确实,文档里面有很多东西!

© www.soinside.com 2019 - 2024. All rights reserved.