Using an API from inside Streamlit app

Too bad. I would +1 Caroline in that Streamlit does not interfere with requests being sent. Wanna give a try and ask the API provider maybe?

Try this code with streamlit. It runs fine locally from my test.

import requests
import streamlit as st


def call_api(
        filename,
        bdata=None,
        overlay=False,
        api_key='helloworld',
        language='eng'):
    payload = {'isOverlayRequired': overlay,
               'apikey': api_key,
               'language': language}

    if bdata is not None:
        r = requests.post(
            'https://api.ocr.space/parse/image',
            files={filename: bdata},
            data=payload)
    else:
        with open(filename, 'rb') as f:
            r = requests.post(
                'https://api.ocr.space/parse/image',
                files={filename: f},
                data=payload)

    return r.content.decode()


if __name__ == '__main__':
    uploaded_file = st.file_uploader("Choose file")

    is_submit = st.button('Process File')
    
    if is_submit and uploaded_file is not None:
        bytes_data = uploaded_file.getvalue()

        ret_json = call_api(
            uploaded_file.name,
            bdata=bytes_data,
            api_key=st.secrets['ocr_api_key']['mykey']  # 'helloworld'
        )

        st.json(ret_json)
2 Likes

oh, i feel so stupid now. The entire time, the issue was that i was passing in an string instead of an integer to one of the parameters. That caused the issue.

Huge thanks to everyone who spent time to try and solve this issue"

1 Like

Glad that you fixed the issue and told us how!
Best,

1 Like

Awesome, glad you got it resolved

I have a bigger problem, I’m extracting data from DigiKala site (an Iranian online store) through Api. This code works well on my laptop, and it shows the output, but as soon as I deploy it on the Stream Lite host, the code runs, but it stays in running mode and does not show the output.
Streamlit link: https://digikalascraper.streamlit.app/
GitHub link: GitHub - qashqaeii/digikala_scaper: This script is a web scraper built specifically for extracting information about mobile phone products from the Digikala website. It utilizes the Digikala API to retrieve data and provides options for selecting brand, sort, and availability preferences. The extracted data is displayed in a table and can be downloaded as an Excel file.
Please help me to solve this problem.

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