prompt: We are currently solving the following issue within our repository. Here is the issue text:
  --- BEGIN ISSUE ---
  st.write throws value is null when string is too big
  # Summary
  calling `st.write` with a string that is too big will result on an error message on the front end with message "value is null"
  
  <img width="551" alt="Screen Shot 2019-10-16 at 3 57 57 PM" src="https://user-images.githubusercontent.com/934511/66950384-a17d0e00-f02e-11e9-87bb-1df7158794b5.png">
  
  # Steps to reproduce
  1. create a script with a variable that holds a string that's more than weights more than 50mb
  2. streamlit run yourscript.py
  3. message will appear on the front end
  
  
  ## Expected behavior:
  The error message should be clearer.
  
  ## Actual behavior:
  Error message does not point to the string length constrain of `st.write`.
  
  
  
  --- END ISSUE ---
  
  Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
  --- BEGIN FILES ---
  Path: `lib/streamlit/server/server_util.py`
  Content:
  ```
  1 # Copyright 2018-2020 Streamlit Inc.
  2 #
  3 # Licensed under the Apache License, Version 2.0 (the "License");
  4 # you may not use this file except in compliance with the License.
  5 # You may obtain a copy of the License at
  6 #
  7 #    http://www.apache.org/licenses/LICENSE-2.0
  8 #
  9 # Unless required by applicable law or agreed to in writing, software
  10 # distributed under the License is distributed on an "AS IS" BASIS,
  11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12 # See the License for the specific language governing permissions and
  13 # limitations under the License.
  14 
  15 """Server related utility functions"""
  16 
  17 from typing import Callable, List, Optional, Union
  18 
  19 from streamlit import config
  20 from streamlit import net_util
  21 from streamlit import type_util
  22 from streamlit import url_util
  23 from streamlit.ForwardMsgCache import populate_hash_if_needed
  24 
  25 # Largest message that can be sent via the WebSocket connection.
  26 # (Limit was picked arbitrarily)
  27 # TODO: Break message in several chunks if too large.
  28 MESSAGE_SIZE_LIMIT = 50 * 1e6  # 50MB
  29 
  30 
  31 def is_cacheable_msg(msg):
  32     """True if the given message qualifies for caching.
  33 
  34     Parameters
  35     ----------
  36     msg : ForwardMsg
  37 
  38     Returns
  39     -------
  40     bool
  41         True if we should cache the message.
  42 
  43     """
  44     if msg.WhichOneof("type") in {"ref_hash", "initialize"}:
  45         # Some message types never get cached
  46         return False
  47     return msg.ByteSize() >= config.get_option("global.minCachedMessageSize")
  48 
  49 
  50 def serialize_forward_msg(msg):
  51     """Serialize a ForwardMsg to send to a client.
  52 
  53     If the message is too large, it will be converted to an exception message
  54     instead.
  55 
  56     Parameters
  57     ----------
  58     msg : ForwardMsg
  59         The message to serialize
  60 
  61     Returns
  62     -------
  63     str
  64         The serialized byte string to send
  65 
  66     """
  67     populate_hash_if_needed(msg)
  68     msg_str = msg.SerializeToString()
  69 
  70     if len(msg_str) > MESSAGE_SIZE_LIMIT:
  71         _convert_msg_to_exception_msg(msg, RuntimeError("Data too large"))
  72         msg_str = msg.SerializeToString()
  73 
  74     return msg_str
  75 
  76 
  77 def _convert_msg_to_exception_msg(msg, e):
  78     import streamlit.elements.exception_proto as exception_proto
  79 
  80     delta_id = msg.metadata.delta_id
  81     msg.Clear()
  82     msg.metadata.delta_id = delta_id
  83 
  84     exception_proto.marshall(msg.delta.new_element.exception, e)
  85 
  86 
  87 def is_url_from_allowed_origins(url):
  88     """Return True if URL is from allowed origins (for CORS purpose).
  89 
  90     Allowed origins:
  91     1. localhost
  92     2. The internal and external IP addresses of the machine where this
  93        function was called from.
  94     3. The cloud storage domain configured in `s3.bucket`.
  95 
  96     If `server.enableCORS` is False, this allows all origins.
  97 
  98     Parameters
  99     ----------
  100     url : str
  101         The URL to check
  102 
  103     Returns
  104     -------
  105     bool
  106         True if URL is accepted. False otherwise.
  107 
  108     """
  109     if not config.get_option("server.enableCORS"):
  110         # Allow everything when CORS is disabled.
  111         return True
  112 
  113     hostname = url_util.get_hostname(url)
  114 
  115     allowed_domains = [  # List[Union[str, Callable[[], Optional[str]]]]
  116         # Check localhost first.
  117         "localhost",
  118         "0.0.0.0",
  119         "127.0.0.1",
  120         # Try to avoid making unecessary HTTP requests by checking if the user
  121         # manually specified a server address.
  122         _get_server_address_if_manually_set,
  123         _get_s3_url_host_if_manually_set,
  124         # Then try the options that depend on HTTP requests or opening sockets.
  125         net_util.get_internal_ip,
  126         net_util.get_external_ip,
  127         lambda: config.get_option("s3.bucket"),
  128     ]
  129 
  130     for allowed_domain in allowed_domains:
  131         if callable(allowed_domain):
  132             allowed_domain = allowed_domain()
  133 
  134         if allowed_domain is None:
  135             continue
  136 
  137         if hostname == allowed_domain:
  138             return True
  139 
  140     return False
  141 
  142 
  143 def _get_server_address_if_manually_set() -> Optional[str]:
  144     if config.is_manually_set("browser.serverAddress"):
  145         return url_util.get_hostname(config.get_option("browser.serverAddress"))
  146     return None
  147 
  148 
  149 def _get_s3_url_host_if_manually_set() -> Optional[str]:
  150     if config.is_manually_set("s3.url"):
  151         return url_util.get_hostname(config.get_option("s3.url"))
  152     return None
  153 
  154 
  155 def make_url_path_regex(*path, **kwargs):
  156     """Get a regex of the form ^/foo/bar/baz/?$ for a path (foo, bar, baz)."""
  157     path = [x.strip("/") for x in path if x]  # Filter out falsy components.
  158     path_format = r"^/%s/?$" if kwargs.get("trailing_slash", True) else r"^/%s$"
  159     return path_format % "/".join(path)
  160
  ```
  --- END FILES ---
  
  Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.
  
  Here is an example:
  
  ```diff
  diff --git a/examples/server_async.py b/examples/server_async.py
  --- a/examples/server_async.py
  +++ b/examples/server_async.py
  @@ -313,4 +313,4 @@
   
   
   if __name__ == "__main__":
  -    asyncio.run(run_async_server("."), debug=True)
  +    asyncio.run(run_async_server(), debug=True)
  diff --git a/examples/server_sync.py b/examples/server_sync.py
  --- a/examples/server_sync.py
  +++ b/examples/server_sync.py
  @@ -313,5 +313,5 @@
   
   
   if __name__ == "__main__":
  -    server = run_sync_server(".")
  +    server = run_sync_server()
       server.shutdown()
  
  ```
  
golden_diff: diff --git a/lib/streamlit/server/server_util.py b/lib/streamlit/server/server_util.py
  --- a/lib/streamlit/server/server_util.py
  +++ b/lib/streamlit/server/server_util.py
  @@ -68,22 +68,19 @@
       msg_str = msg.SerializeToString()
   
       if len(msg_str) > MESSAGE_SIZE_LIMIT:
  -        _convert_msg_to_exception_msg(msg, RuntimeError("Data too large"))
  +        import streamlit.elements.exception_proto as exception_proto
  +
  +        error = RuntimeError(
  +            f"Data of size {len(msg_str)/1e6:.1f}MB exceeds write limit of {MESSAGE_SIZE_LIMIT/1e6}MB"
  +        )
  +        # Overwrite the offending ForwardMsg.delta with an error to display.
  +        # This assumes that the size limit wasn't exceeded due to metadata.
  +        exception_proto.marshall(msg.delta.new_element.exception, error)
           msg_str = msg.SerializeToString()
   
       return msg_str
   
   
  -def _convert_msg_to_exception_msg(msg, e):
  -    import streamlit.elements.exception_proto as exception_proto
  -
  -    delta_id = msg.metadata.delta_id
  -    msg.Clear()
  -    msg.metadata.delta_id = delta_id
  -
  -    exception_proto.marshall(msg.delta.new_element.exception, e)
  -
  -
   def is_url_from_allowed_origins(url):
       """Return True if URL is from allowed origins (for CORS purpose).
  
verification_info: {"golden_diff": "diff --git a/lib/streamlit/server/server_util.py b/lib/streamlit/server/server_util.py\n--- a/lib/streamlit/server/server_util.py\n+++ b/lib/streamlit/server/server_util.py\n@@ -68,22 +68,19 @@\n     msg_str = msg.SerializeToString()\n \n     if len(msg_str) > MESSAGE_SIZE_LIMIT:\n-        _convert_msg_to_exception_msg(msg, RuntimeError(\"Data too large\"))\n+        import streamlit.elements.exception_proto as exception_proto\n+\n+        error = RuntimeError(\n+            f\"Data of size {len(msg_str)/1e6:.1f}MB exceeds write limit of {MESSAGE_SIZE_LIMIT/1e6}MB\"\n+        )\n+        # Overwrite the offending ForwardMsg.delta with an error to display.\n+        # This assumes that the size limit wasn't exceeded due to metadata.\n+        exception_proto.marshall(msg.delta.new_element.exception, error)\n         msg_str = msg.SerializeToString()\n \n     return msg_str\n \n \n-def _convert_msg_to_exception_msg(msg, e):\n-    import streamlit.elements.exception_proto as exception_proto\n-\n-    delta_id = msg.metadata.delta_id\n-    msg.Clear()\n-    msg.metadata.delta_id = delta_id\n-\n-    exception_proto.marshall(msg.delta.new_element.exception, e)\n-\n-\n def is_url_from_allowed_origins(url):\n     \"\"\"Return True if URL is from allowed origins (for CORS purpose).\n", "issue": "st.write throws value is null when string is too big\n# Summary\r\ncalling `st.write` with a string that is too big will result on an error message on the front end with message \"value is null\"\r\n\r\n<img width=\"551\" alt=\"Screen Shot 2019-10-16 at 3 57 57 PM\" src=\"https://user-images.githubusercontent.com/934511/66950384-a17d0e00-f02e-11e9-87bb-1df7158794b5.png\">\r\n\r\n# Steps to reproduce\r\n1. create a script with a variable that holds a string that's more than weights more than 50mb\r\n2. streamlit run yourscript.py\r\n3. message will appear on the front end\r\n\r\n\r\n## Expected behavior:\r\nThe error message should be clearer.\r\n\r\n## Actual behavior:\r\nError message does not point to the string length constrain of `st.write`.\r\n\r\n\n", "before_files": [{"content": "# Copyright 2018-2020 Streamlit Inc.\n#\n# Licensed under the Apache License, Version 2.0 (the \"License\");\n# you may not use this file except in compliance with the License.\n# You may obtain a copy of the License at\n#\n#    http://www.apache.org/licenses/LICENSE-2.0\n#\n# Unless required by applicable law or agreed to in writing, software\n# distributed under the License is distributed on an \"AS IS\" BASIS,\n# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n# See the License for the specific language governing permissions and\n# limitations under the License.\n\n\"\"\"Server related utility functions\"\"\"\n\nfrom typing import Callable, List, Optional, Union\n\nfrom streamlit import config\nfrom streamlit import net_util\nfrom streamlit import type_util\nfrom streamlit import url_util\nfrom streamlit.ForwardMsgCache import populate_hash_if_needed\n\n# Largest message that can be sent via the WebSocket connection.\n# (Limit was picked arbitrarily)\n# TODO: Break message in several chunks if too large.\nMESSAGE_SIZE_LIMIT = 50 * 1e6  # 50MB\n\n\ndef is_cacheable_msg(msg):\n    \"\"\"True if the given message qualifies for caching.\n\n    Parameters\n    ----------\n    msg : ForwardMsg\n\n    Returns\n    -------\n    bool\n        True if we should cache the message.\n\n    \"\"\"\n    if msg.WhichOneof(\"type\") in {\"ref_hash\", \"initialize\"}:\n        # Some message types never get cached\n        return False\n    return msg.ByteSize() >= config.get_option(\"global.minCachedMessageSize\")\n\n\ndef serialize_forward_msg(msg):\n    \"\"\"Serialize a ForwardMsg to send to a client.\n\n    If the message is too large, it will be converted to an exception message\n    instead.\n\n    Parameters\n    ----------\n    msg : ForwardMsg\n        The message to serialize\n\n    Returns\n    -------\n    str\n        The serialized byte string to send\n\n    \"\"\"\n    populate_hash_if_needed(msg)\n    msg_str = msg.SerializeToString()\n\n    if len(msg_str) > MESSAGE_SIZE_LIMIT:\n        _convert_msg_to_exception_msg(msg, RuntimeError(\"Data too large\"))\n        msg_str = msg.SerializeToString()\n\n    return msg_str\n\n\ndef _convert_msg_to_exception_msg(msg, e):\n    import streamlit.elements.exception_proto as exception_proto\n\n    delta_id = msg.metadata.delta_id\n    msg.Clear()\n    msg.metadata.delta_id = delta_id\n\n    exception_proto.marshall(msg.delta.new_element.exception, e)\n\n\ndef is_url_from_allowed_origins(url):\n    \"\"\"Return True if URL is from allowed origins (for CORS purpose).\n\n    Allowed origins:\n    1. localhost\n    2. The internal and external IP addresses of the machine where this\n       function was called from.\n    3. The cloud storage domain configured in `s3.bucket`.\n\n    If `server.enableCORS` is False, this allows all origins.\n\n    Parameters\n    ----------\n    url : str\n        The URL to check\n\n    Returns\n    -------\n    bool\n        True if URL is accepted. False otherwise.\n\n    \"\"\"\n    if not config.get_option(\"server.enableCORS\"):\n        # Allow everything when CORS is disabled.\n        return True\n\n    hostname = url_util.get_hostname(url)\n\n    allowed_domains = [  # List[Union[str, Callable[[], Optional[str]]]]\n        # Check localhost first.\n        \"localhost\",\n        \"0.0.0.0\",\n        \"127.0.0.1\",\n        # Try to avoid making unecessary HTTP requests by checking if the user\n        # manually specified a server address.\n        _get_server_address_if_manually_set,\n        _get_s3_url_host_if_manually_set,\n        # Then try the options that depend on HTTP requests or opening sockets.\n        net_util.get_internal_ip,\n        net_util.get_external_ip,\n        lambda: config.get_option(\"s3.bucket\"),\n    ]\n\n    for allowed_domain in allowed_domains:\n        if callable(allowed_domain):\n            allowed_domain = allowed_domain()\n\n        if allowed_domain is None:\n            continue\n\n        if hostname == allowed_domain:\n            return True\n\n    return False\n\n\ndef _get_server_address_if_manually_set() -> Optional[str]:\n    if config.is_manually_set(\"browser.serverAddress\"):\n        return url_util.get_hostname(config.get_option(\"browser.serverAddress\"))\n    return None\n\n\ndef _get_s3_url_host_if_manually_set() -> Optional[str]:\n    if config.is_manually_set(\"s3.url\"):\n        return url_util.get_hostname(config.get_option(\"s3.url\"))\n    return None\n\n\ndef make_url_path_regex(*path, **kwargs):\n    \"\"\"Get a regex of the form ^/foo/bar/baz/?$ for a path (foo, bar, baz).\"\"\"\n    path = [x.strip(\"/\") for x in path if x]  # Filter out falsy components.\n    path_format = r\"^/%s/?$\" if kwargs.get(\"trailing_slash\", True) else r\"^/%s$\"\n    return path_format % \"/\".join(path)\n", "path": "lib/streamlit/server/server_util.py"}], "after_files": [{"content": "# Copyright 2018-2020 Streamlit Inc.\n#\n# Licensed under the Apache License, Version 2.0 (the \"License\");\n# you may not use this file except in compliance with the License.\n# You may obtain a copy of the License at\n#\n#    http://www.apache.org/licenses/LICENSE-2.0\n#\n# Unless required by applicable law or agreed to in writing, software\n# distributed under the License is distributed on an \"AS IS\" BASIS,\n# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n# See the License for the specific language governing permissions and\n# limitations under the License.\n\n\"\"\"Server related utility functions\"\"\"\n\nfrom typing import Callable, List, Optional, Union\n\nfrom streamlit import config\nfrom streamlit import net_util\nfrom streamlit import type_util\nfrom streamlit import url_util\nfrom streamlit.ForwardMsgCache import populate_hash_if_needed\n\n# Largest message that can be sent via the WebSocket connection.\n# (Limit was picked arbitrarily)\n# TODO: Break message in several chunks if too large.\nMESSAGE_SIZE_LIMIT = 50 * 1e6  # 50MB\n\n\ndef is_cacheable_msg(msg):\n    \"\"\"True if the given message qualifies for caching.\n\n    Parameters\n    ----------\n    msg : ForwardMsg\n\n    Returns\n    -------\n    bool\n        True if we should cache the message.\n\n    \"\"\"\n    if msg.WhichOneof(\"type\") in {\"ref_hash\", \"initialize\"}:\n        # Some message types never get cached\n        return False\n    return msg.ByteSize() >= config.get_option(\"global.minCachedMessageSize\")\n\n\ndef serialize_forward_msg(msg):\n    \"\"\"Serialize a ForwardMsg to send to a client.\n\n    If the message is too large, it will be converted to an exception message\n    instead.\n\n    Parameters\n    ----------\n    msg : ForwardMsg\n        The message to serialize\n\n    Returns\n    -------\n    str\n        The serialized byte string to send\n\n    \"\"\"\n    populate_hash_if_needed(msg)\n    msg_str = msg.SerializeToString()\n\n    if len(msg_str) > MESSAGE_SIZE_LIMIT:\n        import streamlit.elements.exception_proto as exception_proto\n\n        error = RuntimeError(\n            f\"Data of size {len(msg_str)/1e6:.1f}MB exceeds write limit of {MESSAGE_SIZE_LIMIT/1e6}MB\"\n        )\n        # Overwrite the offending ForwardMsg.delta with an error to display.\n        # This assumes that the size limit wasn't exceeded due to metadata.\n        exception_proto.marshall(msg.delta.new_element.exception, error)\n        msg_str = msg.SerializeToString()\n\n    return msg_str\n\n\ndef is_url_from_allowed_origins(url):\n    \"\"\"Return True if URL is from allowed origins (for CORS purpose).\n\n    Allowed origins:\n    1. localhost\n    2. The internal and external IP addresses of the machine where this\n       function was called from.\n    3. The cloud storage domain configured in `s3.bucket`.\n\n    If `server.enableCORS` is False, this allows all origins.\n\n    Parameters\n    ----------\n    url : str\n        The URL to check\n\n    Returns\n    -------\n    bool\n        True if URL is accepted. False otherwise.\n\n    \"\"\"\n    if not config.get_option(\"server.enableCORS\"):\n        # Allow everything when CORS is disabled.\n        return True\n\n    hostname = url_util.get_hostname(url)\n\n    allowed_domains = [  # List[Union[str, Callable[[], Optional[str]]]]\n        # Check localhost first.\n        \"localhost\",\n        \"0.0.0.0\",\n        \"127.0.0.1\",\n        # Try to avoid making unecessary HTTP requests by checking if the user\n        # manually specified a server address.\n        _get_server_address_if_manually_set,\n        _get_s3_url_host_if_manually_set,\n        # Then try the options that depend on HTTP requests or opening sockets.\n        net_util.get_internal_ip,\n        net_util.get_external_ip,\n        lambda: config.get_option(\"s3.bucket\"),\n    ]\n\n    for allowed_domain in allowed_domains:\n        if callable(allowed_domain):\n            allowed_domain = allowed_domain()\n\n        if allowed_domain is None:\n            continue\n\n        if hostname == allowed_domain:\n            return True\n\n    return False\n\n\ndef _get_server_address_if_manually_set() -> Optional[str]:\n    if config.is_manually_set(\"browser.serverAddress\"):\n        return url_util.get_hostname(config.get_option(\"browser.serverAddress\"))\n    return None\n\n\ndef _get_s3_url_host_if_manually_set() -> Optional[str]:\n    if config.is_manually_set(\"s3.url\"):\n        return url_util.get_hostname(config.get_option(\"s3.url\"))\n    return None\n\n\ndef make_url_path_regex(*path, **kwargs):\n    \"\"\"Get a regex of the form ^/foo/bar/baz/?$ for a path (foo, bar, baz).\"\"\"\n    path = [x.strip(\"/\") for x in path if x]  # Filter out falsy components.\n    path_format = r\"^/%s/?$\" if kwargs.get(\"trailing_slash\", True) else r\"^/%s$\"\n    return path_format % \"/\".join(path)\n", "path": "lib/streamlit/server/server_util.py"}]}