Error with an empty line of code

Hi,
Every time I try to run a specific part of my script, the following error is displayed:

File "/Users/tiagocoutinho/Desktop/streamlit/lib/python3.9/site-packages/streamlit/scriptrunner/script_runner.py", line 475, in _run_script
    exec(code, module.__dict__)

As you can see line 475 is empty.


Why does this happen?

This code snippet is without any context. I assume there is a syntax error somewhere else.

I am trying to create a PDF that must include a figure with a plot.
This is the code that I am using to create the figure.

fig1, (ax1, ax2) = plt.subplots(1, 2)

hip_angle, _, _ = angle_calculation(merged)
hip_angle_2, _, _ = angle_calculation(merged_2)

sub_plotting(freq_left, hip_angle_2, 'Hip angle - left', ax1)
sub_plotting(freq_right, hip_angle, 'Hip angle - right', ax2)
ax1.set_xlabel('Gait phase %')
ax2.set_xlabel('Gait phase %')
ax1.set_ylabel('Angle (degrees)')
ax2.set_ylabel('Angle (degrees)')
ax1.set_xticks(np.arange(0, 110, step=10))
ax2.set_xticks(np.arange(0, 110, step=10))
ax1.set_ylim([-20, 50])
ax2.set_ylim([-20, 50])

ax1.text(1, -6, 'Flexion', fontsize=10)
ax1.text(1, -8, 'Extension', fontsize=10)
ax1.arrow(19.5, -6, 0, 1, width=0.07, color='black', head_width=0.5)
ax1.arrow(19.5, -8, 0, -1, width=0.07, color='black', head_width=0.5)

ax2.text(1, -6, 'Flexion', fontsize=10)
ax2.text(1, -8, ' Extension', fontsize=10)
ax2.arrow(19.5, -6, 0, 1, width=0.07, color='black', head_width=0.5)
ax2.arrow(19.5, -8, 0, -1, width=0.07, color='black', head_width=0.5)

ax1.set_aspect('auto')
ax2.set_aspect('auto')
fig1.set_figheight(8)
fig1.set_figwidth(18)
st.pyplot(fig1)

This is the function sub_plotting used in the code above.

def sub_plotting(x, y, title, ax=None, xlabel='Gait phase (%)', ylabel='Distance'):
    if ax is None:
        ax = plt.gca()
    plot = ax.plot(x, y, linewidth=2, color='red')
    ax.set_xticks(np.arange(0, int(max(x) + max(x) * 0.1), int((max(x) / 10))))
    ax.set_xlabel(xlabel)
    ax.margins(0, 0)
    ax.set_ylabel(ylabel)
    ax.set_title(title)
    ax.grid(True)
    return plot

The code above is working. The figure with the plot is displayed in the app.
The problem appears here. I am using this code to create the PDF file with the figure.

def create_pdf(figure1):

    pdf = FPDF()
    pdf.add_page()
    figure = io.BytesIO()
    figure1.savefig(figure, format="png")
    saved_fig = tempfile.NamedTemporaryFile()

    with open(f"{saved_fig.name}.png", 'wb') as sf:
        sf.write(figure.getvalue())

    pdf.set_xy(30, 50)
    pdf.image(figure1, w=140, h=110)
    figure.close()

    return bytes(pdf.output())

When I try to use the fig1 as the argument of create_pdf function this error is displayed.

NameError: name 'fig1' is not defined

Traceback:

File "/Users/tiagocoutinho/Desktop/streamlit/lib/python3.9/site-packages/streamlit/scriptrunner/script_runner.py", line 475, in _run_script
    exec(code, module.__dict__)
File "/Users/tiagocoutinho/Desktop/streamlit/main.py", line 724, in <module>
    data=create_pdf(fig1),

But as I mentioned in the first post, line 475 is empty.
The function create_pdf is called after fig1 has been defined.

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.